Factory Design Pattern in Java

Introduction

Factory pattern is one of the most used design pattern in Java. This type of design pattern comes under creational pattern as this pattern provides one of the best ways to create an object. In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface. Factory design pattern is used when we have a super class with multiple subclasses and based on input, we need to return one of the sub-class. This pattern take out the responsibility of instantiation of a class from client program to the factory class. Super class in factory pattern can be an interface, abstract class or a normal java class.

Implementation

We will create a Building interface and concrete classes implementing the Building interface. A factory class BuildingFactory is defined as a next step. The demo class, FactoryPatternDemo, will use BuildingFactory to get a Building object. It will pass information (Home / House / Hut) to BuildingFactory to get the type of object it needs.

Class Diagram

Factory Pattern in JEE
The following steps illustrate how to create factory pattern

Step 1. Create a Building interface. This interface has only one method build() which will be implemented by different classes to build different kind of buildings.

package com.roytuts.designpattern.factory;
public interface Building {
  void build();
}

Step 2. Create concrete classes to implement the Building interface.
The below class builds a Home.

package com.roytuts.designpattern.factory;
public class Home implements Building {
  @Override
  public void build() {
    System.out.println("Building Home");
  }
}

The below class builds a House.

package com.roytuts.designpattern.factory;
public class House implements Building {
  @Override
  public void build() {
    System.out.println("Building House");
  }
}

The below class builds a Hut.

package com.roytuts.designpattern.factory;
public class Hut implements Building {
  @Override
  public void build() {
    System.out.println("Building Hut");
  }
}

Step 3. Create a BuildingFactory to generate object of concrete class based on given information.

package com.roytuts.designpattern.factory;
public class BuildingFactory {
  public Building getBuilding(String buildingType) {
    if (buildingType != null) {
      if ("HOME".equalsIgnoreCase(buildingType)) {
        return new Home();
      }
      if ("HOUSE".equalsIgnoreCase(buildingType)) {
        return new House();
      }
      if ("HUT".equalsIgnoreCase(buildingType)) {
        return new Hut();
      }
    }
    return null;
  }
}

Step 4. Use the BuildingFactory to get object of concrete class by passing an information such as building type.

package com.roytuts.designpattern.factory;
public class FactoryPatternDemo {
  /**
   * @param args
   */
  public static void main(String[] args) {
    BuildingFactory buildingFactory = new BuildingFactory();
    Building building1 = buildingFactory.getBuilding("Home");
    building1.build();
    Building building2 = buildingFactory.getBuilding("House");
    building2.build();
    Building building3 = buildingFactory.getBuilding("Hut");
    building3.build();
  }
}

Step 5. Run FactoryPatternDemo. You will see the below output

Building Home
Building House
Building Hut

Advantages

Factory pattern provides approach to code for interface rather than implementation.
Factory pattern removes the instantiation of actual implementation classes from client code, making it more robust, less coupled and easy to extend.
Factory pattern provides abstraction between implementation and client classes through inheritance.

That’s all. Thank you for your reading.

Leave a Reply

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