jobs4timesLogo jobs4timesLogo

Factory Method Design Pattern

  • Factory method is used for creating the object for family of related classes with in the hierarchy.
  • Let's consider for example Maruthi is manufacturing car's. It manufactures several types of car's.
  • It has several manufacturing units in which the cars are being manufactured.
package design;

public abstract class Car {
 	protected int engineNo;
 	protected String color;
 	
 	//setters and getters
 	//toString()
 	 	
 //every car has its own driving style
 public abstract void drive();
}
package design;

public class AltoCar extends Car{

@Override
public void drive() {
 System.out.println("driving alto car");
}
	
}
package design;

public class WagnorCar extends Car{

@Override
public void drive() {
 System.out.println("driving wagnor car");
}
	
}
  • These car's will be manufactured across various maruthi manufacturing units across india. Every manufacturing unit may not manufacture all the models.

  • For e.g. GujaratMaruthi manufacturing unit only manufactures alto cars and PuneMaruthi manufacturing unit only manufactures Waganor and Swift.
package design;

public class PuneWorkshop{

public Car createCar(String type){
	Car car=null;

 if(type.equals("wagnor")){
	 car=new WagnorCar();
 }
	 
  //creating and assembly
 System.out.println("assemble engine");
	 
return car;
}

}
package design;

public class GujratWorkshop{

public Car createCar(String type){
	Car car=null;

 if(type.equals("alto")){
	 car=new AltoCar();
 }
	 
  //creating and assembly
 System.out.println("attach wheels");
 System.out.println("fit seats");
 System.out.println("assemble engine");
	 
return car;
}

}
  • The problem in the above example is after creating the car, every manufacturing unit is using their own mechanism of assembling the car.

  • Few are attaching wheels, seats and engine. Few others are forgetting to attach wheels and seats and delivering the car with engine.

  • So in the above we want to bring up some quality control and standardization of assembling a car.
    Create a MaruthiWorkshop which takes care of standardizing the process of assembling the car.

package design;

public abstract class MaruthiWorkshop{

public Car assembly(String type){
	Car car=null;

  car=createCar(type);
	 
  //creating and assembly
 System.out.println("attach wheels");
 System.out.println("fit seals");
 System.out.println("assemble engine");
	 
return car;
}

protected abstract Car createCar(String type);

}
  • The advantage with the above approach is every Workshop will take care of manufacturing the cars. But the complete control of assembly and delivering the car will be done across the workshops in the same manner as it is controlled by the super class.

  • And now our factory method createCar in the above class can create objects of only the sub-classes of car class only(in the hierarchy).

    Even we add more car's in future also the assembling and delivering of those car's will not get affected.

    More over the code deals with super type, so it can work with any user-defined ConcreateCar types.


Factory Method Design Pattern

BACK