Design Patterns – 02 – Factory Method (Virtual Constructor) Pattern

Merhaba Arkadaslar
Bu bolumde Factory Pattern / Factory Method Pattern’i inceleyecegiz.

Gang of Four’da Factory Method Pattern icin su tanim gecer;
Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a class defer instantiation to subclasses

Factory Method Pattern’ni Virtual Constructor Pattern olarak da bilinir.

Model

Car.java

Car tipinde interface tanimlayalim.

package _02.factory.method.pattern.model;

public interface Car {

	public String getModel();

	public double getPrice();
	
	public void speedUp();

}

Car interface’sini uygulayan siniflarimiz olacak , ihtiyaca gore yeni gelen araba tiplerine gore yeni siniflar eklenebilir.

  • Hatchback
  • Sedan
  • StationWagon

Hatchback.java

package _02.factory.method.pattern.model;

public class Hatchback implements Car {

	@Override
	public String getModel() {
		return "Hatchback";
	}

	@Override
	public double getPrice() {
		return 1000;
	}

	@Override
	public void speedUp() {
		System.out.println("Hatchback#speedUp");
	}

}

Sedan.java

package _02.factory.method.pattern.model;

public class Sedan implements Car {

	@Override
	public String getModel() {
		return "Sedan";
	}

	@Override
	public double getPrice() {
		return 2000;
	}

	@Override
	public void speedUp() {
		System.out.println("Sedan#speedUp");
	}

}

StationWagon.java

package _02.factory.method.pattern.model;

public class StationWagon implements Car {

	@Override
	public String getModel() {
		return "StationWagon";
	}

	@Override
	public double getPrice() {
		return 1500;
	}

	@Override
	public void speedUp() {
		System.out.println("StationWagon#speedUp");
	}

}

CarType.java
Okunabilirlik acisindan CarType enum tanimliyoruz.

package _02.factory.method.pattern.model;

public enum CarType {
	HATCHBACK, SEDAN, STATION_WAGON;
}

Factory

CarFactory.java
CarFactory sinifimizda static getCarInstance metodu tanimlayalim. Bu metot parametre olarak CarType almaktadir , ilgili parametreye gore geriye Hatchback , Sedan , StationWagon objelerini olusturup donmekteyiz.

Bu metot geriye Car tipinde donmektedir , hangi objenin geriye donecegi calisma zamaninda (runtime) ilgili paremetreye gore belirlenecektir. Burada Sedan , Hatchback , StationWagon siniflari Car arabirimini uygulamak zorundadir.

Bu algoritmayi switch/case ile yapabilecegimiz gibi if-else ile de yapabiliriz.

package _02.factory.method.pattern.factory;

import _02.factory.method.pattern.model.Car;
import _02.factory.method.pattern.model.CarType;
import _02.factory.method.pattern.model.Hatchback;
import _02.factory.method.pattern.model.Sedan;
import _02.factory.method.pattern.model.StationWagon;

public class CarFactory {

	public static Car getCarInstance(CarType carType) {

		switch (carType) {
		case HATCHBACK:
			return new Hatchback();
		case SEDAN:
			return new Sedan();
		case STATION_WAGON:
			return new StationWagon();
		default:
			return null;
		}
		
		// we can use switch-case or if-else

//		if (CarType.HATCHBACK == carType) {
//			return new Hatchback();
//		} else if (CarType.SEDAN == carType) {
//			return new Sedan();
//		} else if (CarType.STATION_WAGON == carType) {
//			return new StationWagon();
//		} else {
//			return null;
//		}

	}
}

Run Application

FactoryMethodTest.java
Ornegimizi inceleyelim , sirasiyla SEDAN , HATCHBACK ve STATION_WAGON , CarType olarak getCarInstance metoduna veriyoruz.

package _02.factory.method.pattern.test;

import _02.factory.method.pattern.factory.CarFactory;
import _02.factory.method.pattern.model.Car;
import _02.factory.method.pattern.model.CarType;

public class FactoryMethodTest {

	public static void main(String[] args) {

		Car sedan = CarFactory.getCarInstance(CarType.SEDAN);
		System.out.println(sedan.getModel());
		System.out.println(sedan.getPrice());
		sedan.speedUp();
		System.out.println();

		Car hatchback = CarFactory.getCarInstance(CarType.HATCHBACK);
		System.out.println(hatchback.getModel());
		System.out.println(hatchback.getPrice());
		hatchback.speedUp();
		System.out.println();

		Car stationWagon = CarFactory.getCarInstance(CarType.STATION_WAGON);
		System.out.println(stationWagon.getModel());
		System.out.println(stationWagon.getPrice());
		stationWagon.speedUp();
		System.out.println();

	}
}

Sedan
2000.0
Sedan#speedUp

Hatchback
1000.0
Hatchback#speedUp

StationWagon
1500.0
StationWagon#speedUp


Github kaynak kodlar / source folder
leventerguder/injavawetrust.designpattern

Yazimi burada sonlandiriyorum.
Herkese bol Javali gunler dilerim.
Be an oracle man , import java.*;
Levent Erguder
OCP, Java SE 6 Programmer
OCE, Java EE 6 Web Component Developer

Print Friendly, PDF & Email

Leave a Reply

Your email address will not be published. Required fields are marked *