Design Patterns - Factory

design-patterns

// Design Pattern - Factory:
// Create object without exposing the creation logic to the client and 
// refer to newly created object using a common interface.

// Step 1: Create an interface
public interface Shape {
   void draw();
}

// Step2: Create concrete classes implementing the interface:
public class Rectangle implements Shape {
   public void draw() {
      System.out.println("Inside Rectangle::draw() method.");
   }
}

public class Square implements Shape {
   public void draw() {
      System.out.println("Inside Square::draw() method.");
   }
}

public class Circle implements Shape {
   public void draw() {
      System.out.println("Inside Circle::draw() method.");
   }
}

// Step 3: Create a Factory to generate object of concrete classes
public class ShapeFactory {

   //use getShape method to get object of type shape 
   public Shape getShape(String shapeType){
      if(shapeType == null){
         return null;
      }        
      if(shapeType.equalsIgnoreCase("CIRCLE")){
         return new Circle();
      } else if(shapeType.equalsIgnoreCase("RECTANGLE")){
         return new Rectangle();
      } else if(shapeType.equalsIgnoreCase("SQUARE")){
         return new Square();
      }
      return null;
   }
}

// Step 4: Use the factory to create objects of concrete classes.
ShapeFactory shapeFactory = new ShapeFactory();
Shape shape1 = shapeFactory.getShape("CIRCLE");
Shape shape2 = shapeFactory.getShape("RECTANGLE");
Shape shape3 = shapeFactory.getShape("SQUARE");

There are two possible benefits:

1. If you need to change, rename, or replace the Automobile class later 
    on you can do so and you will only have to modify the code in the factory, 
    instead of every place in your project that uses the Automobile class.
2. If creating the object is a complicated job you can do all of the work in 
    the factory, instead of repeating it every time you want to create a new instance.

Using the factory pattern isn’t always necessary (or wise). The example code used 
here is so simple that a factory would simply be adding unneeded complexity. 
However if you are making a fairly large or complex project you may save yourself 
a lot of trouble down the road by using factories.

What is the factory pattern?

In this pattern, a class simply creates the object you want to use. Consider the following example of the factory pattern:

class Automobile
{
    private $vehicleMake;
    private $vehicleModel;

    public function __construct($make, $model)
    {
        $this->vehicleMake = $make;
        $this->vehicleModel = $model;
    }

    public function getMakeAndModel()
    {
        return $this->vehicleMake . ' ' . $this->vehicleModel;
    }
}

class AutomobileFactory
{
    public static function create($make, $model)
    {
        return new Automobile($make, $model);
    }
}

In the above code, we have the Automobile class and the AutomobileFactory class.

What are the benefits of using the factory pattern?

There are two possible benefits:

  1. If you need to change, rename, or replace the Automobile class later on you can do so and you will only have to modify the code in the factory, instead of every place in your project that uses the Automobile class.
  2. If creating the object is a complicated job you can do all of the work in the factory, instead of repeating it every time you want to create a new instance.

Using the factory pattern isn’t always necessary (or wise). The example code used here is so simple that a factory would simply be adding unneeded complexity. However if you are making a fairly large or complex project you may save yourself a lot of trouble down the road by using factories.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License