Implementing Factory Method Design Pattern in Java

Factory design pattern and simple factory pattern both are creational design patterns where the factory method is a bit advanced version of simple factory. In simple factory, we move the object creation logic to a separate class and provide a static method that returns the object. In the factory pattern, we move the object creation logic to the subclasses and decide which class to create.

What is the Factory Method?

A factory method is one of the creational design patterns that provides a common interface for creating objects in a superclass, but allows subclasses to alter the type of objects created. An interface for creating objects is defined in Java, but subclasses can alter which object is created by deciding which object to return. It is also known as the factory design pattern.

factory method design pattern in java - codebooker

Implementing a factory pattern in Java

Step 1 - create an interface or an abstract class and define a method which acts as a factory method. This method is contains the logic for object creation.


public abstract class Message {

  public void encrypt() {
    // code to encrypt the message
  }

  public void decrypt() {
    // code to decrypt the message
  }

  public abstract String getMessageBody();
}
  


public abstract class MessageCreator {

  public Message getMessage() {
    Message message = creatMessage();
    // if need use encrypt or decrypt methods here
    return message;
  }

  public abstract Message creatMessage();

}
  

Step 2 - Now create a concrete product class by extending the interface or base class and overriding the factory method, providing the specific implementation logic for creating objects.


public class TextMessage extends Message {

  @Override
  public String getMessageBody() {
    return "This is the message body";
  }

}


public class JSONMessage extends Message {

  @Override
  public String getMessageBody() {
    return "{\\\" message\\\" : \\\"This is message body\\\"}";
  }

}


public class JSONMessageCreator extends MessageCreator {

  @Override
  public Message creatMessage() {
    return new JSONMessage();
  }
  
}


public class TextMessageCreator extends MessageCreator {

  @Override
  public Message creatMessage() {
    return new TextMessage();
  }
  
}

Step 3 - create client code. The client code interacts with the factory method to create objects.


public class Client {

  public static void main(String[] args) {
    printMessageBody(new JSONMessageCreator());
    printMessageBody(new TextMessageCreator());
  }

  public static void printMessageBody(MessageCreator messageCreator) {
    System.out.println(messageCreator.getMessage());
  }
  
}


Output

com.example.designpattern.factorymethod.JSONMessage@58d25a40
com.example.designpattern.factorymethod.TextMessage@368102c8

Consideration of factory class

Consider the below points when implementing the factory method.
  • The base class can be an interface or concrete class.
  • In some cases we need a default object to be in place, so we can also provide default object creation logic in the base class or interface. 
  • As with a simple factory, we can pass additional arguments to the factory method and allow the subclasses to use them to initialize the object.

Post a Comment

Previous Post Next Post

Recent Posts

Facebook