GofPatterns Gofpatterns 





Creational Patterns  «Prev 

Factory Method Pattern in Java

Motivation

This pattern introduces loose coupling between classes which is the most important principle one should consider when designing the application architecture. Loose coupling can be introduced in application architecture by programming against abstract entities rather than concrete implementations. This makes the architecture more flexible.
For our Factory Method Pattern in Java, we will begin with a enum type CarType.java, which will hold the different types of cars and will provide the car types to all other classes.

package com.java.creational.factorymethod;
public enum CarType {
  SMALL, SEDAN, LUXURY
}

Car.java is the Super class of all car instances and it will contain the common logic applicable in creating cars of all types.

package com.java.creational.factorymethod;
public abstract class Car {
 public Car(CarType model) {
   this.model = model;
   arrangeParts();
 }
 private void arrangeParts() {
   // Do one time processing here
 }
 /* Do subclass level processing 
 in this method */
 protected abstract void construct();
 private CarType model = null;
 public CarType getModel() {
   return model;
 }
public void setModel(CarType model) {
 this.model = model;
 }
}


LuxuryCar.java is a concrete implementation of the car type LUXURY.
package com.java.creational.factorymethod;
 
public class LuxuryCar extends Car {
  LuxuryCar() {
    super(CarType.LUXURY);
    construct();
  }
  @Override
  protected void construct() {
    System.out.println("Building luxury car");
    // add accessories
  }
}

SmallCar.java is concrete implementation of car type SMALL
package com.java.creational.factorymethod;
public class SmallCar extends Car { 
  SmallCar() {
    super(CarType.SMALL);
    construct();
  }
  @Override
  protected void construct() {
    System.out.println("Building small car");
    // add accessories
  }
}

SedanCar.java is concrete implementation of car type SEDAN
package com.java.creational.factorymethod;
public class SedanCar extends Car {
  SedanCar(){
    super(CarType.SEDAN);
    construct();
  }
  @Override
  protected void construct() {
    System.out.println("Building sedan car");
    // add accessories
  }
}


CarFactory.java is our main class implemented using factory pattern. It instantiates a car instance only after determining its type.

package com.java.creational.factorymethod;
 
public class CarFactory {
  public static Car buildCar(CarType model) {
    Car car = null;
    switch (model) {
      case SMALL:
        car = new SmallCar();
        break;
      case SEDAN:
        car = new SedanCar();
        break;
      case LUXURY:
        car = new LuxuryCar();
        break;
      default:
      // throw some exception
      break;
    }
    return car;
  }
}

In TestFactoryPattern.java, we will test our factory code. Lets run this class.
package com.java.creational.factorymethod;
 
public class TestFactoryPattern {
  public static void main(String[] args) {
    System.out.println(CarFactory.buildCar(CarType.SMALL));
    System.out.println(CarFactory.buildCar(CarType.SEDAN));
    System.out.println(CarFactory.buildCar(CarType.LUXURY));
  }
}

Output:
Building small car
com.gofpatterns.creational.factory.SmallCar@2ef49ac3
Building sedan car
com.gofpatterns.creational.factory.SedanCar@3cdc904a
Building luxury car
com.gofpatterns.creational.factory.LuxuryCar@3485097d