It’s a Creational design pattern that focuses on object creation process wherein creating product objects is the responsibility of the respective creators. There are 2 parallel class hierarchies defined in this pattern i.e., products and creators/factories.

Purpose: Program to interface, not to implementation and to decentralize the creation of product objects leveraging inheritance and polymorphism, also abide to SOLID design principles.

Let’s take an example of Car product which has specific types like Hatchback, Sedan as shown in the snippet below.

interface Car {
  void make();
}

class Hatchback implements Car {
  @Override
  public void make() {
    System.out.println("Hatchback");
  }
}

class Sedan implements Car {
  @Override
  public void make() {
    System.out.println("Sedan");
  }
}

Initially when writing code, we tend to write code as shown below by creating a specific object based on the passed in enum type.

class Creator {
  public enum CarType {HATCHBACK, SEDAN}
  public Car createCar(CarType carType) { 
    switch (carType) {
      case HATCHBACK:
        return new Hatchback();
      case SEDAN:
        return new Sedan();
      default:
        return null;
    }
  }
}

But, the above snippet is a code smell and not a pattern for the below reason.

  • Creator class shown above violates open-closed principle because every time you add a new car type say Suv, MiniSuv, you have to modify the Creator class adding a new switch case, and updating your enum with new product type.

A better approach to deal with this kind of problem is implementing Factory method pattern in which the creation of sub-product objects is done by the corresponding sub-Creator classes. Here is the code snippet.

interface CarFactory {
  Car createCar();
}

class HatchbackFactory implements CarFactory {
  @Override
  Hatchback createCar() {
    new Hatchback();
  }
}

class SedanFactory implements CarFactory {
  @Override
  Sedan createCar() {
    new Sedan();
  }
}

Sample client code

public class Demo {

  /*-
   * This method is Programmed to interface
   *
   * Addition of new Car products like Suv, MiniSuv
   * doesn't require a change in this method. 
   *
   * Instead new sub-creators have to be created like
   * SuvFactory, MiniSuvFactory which takes care of 
   * Suv, MiniSuv object creation respectively
   */
  static Car getCar(CarFactory carFactory) {
    return carFactory.createCar();
  }

  public static void main(String args[]) {
    Car hatchbag = getCar(new HatchbackFactory());
    Car sedan = getCar(new SedanFactory());
  }
}

Here is UML Class Diagram representation of the above code.

Factory method pattern

Note: It’s a simplified UML without exact class/interfaces structures.

As shown in the above diagram, there are parallel class hierarchies of a Product, and its respective Creators(Factory). Each sub-factory is responsible for creation of specific sub-product.

In conclusion, the factory method pattern delegates the responsibility of creating product objects to its creators.

References:

  1. Factory Method Pattern
  2. Factories