JB Header
Factory Method Design Pattern in Java
This tutorial explains Gang of Four's Factory Method design pattern in java. It starts with the definition of the pattern, which is followed by the class diagram showing the constituent classes of the pattern, and then explains the role played by each these classes. Next an example use case of factory pattern is implemented in Java. Begining with the class diagram for the use case, Java code for its implementation is shown next, which is followed by a detailed explanation of the code. Introduction Factory Method Design Pattern is a creational design pattern among the Gang Of Four(GOF) Design PatternsArticle on GOF Patterns & their types. Being a creational design pattern, the factory method design pattern deals with the creation of a family of objects. What is Factory Method Design Pattern Factory method design pattern creates objects in such a way that it lets the sub-classes decide how to implement the object creation logic.

In the factory pattern, there is a base factory interface/base class which defines a common method for creating objects of subclasses. The actual logic for creation of different type of objects is present in the implementations/subclasses, which determine how to implement the creation logic by overriding the pre-defined instance creation method. Lets have a look at the class diagram of a factory method design pattern to understand it better.
UML Class Diagram for Factory Method Design Pattern
Factory Method Design Pattern Class Diagram
Explanation of the code
  • BaseProduct is the base interfaces for all products.
  • ConcreteProduct implements BaseProduct. There are usually multiple ConcreteProduct implementations for a BaseProduct when a factory design pattern is chosen for object creation.
  • FactoryInterface defines the basic behavior of the factory which in this case is defined by the method getInstance().
  • ConcreteFactory implements FactoryInterface and provides implementation for the getInstance() method. There can be mutiple ConcreteFactory sub-classes.
  • The ConcreteFactory’s getInstance() method creates an object of one of the ConcreteProduct classes. It however sends back an object of type BaseProduct.
  • The actual object created is as per the client invocation context. I.e. if the client asks ConcreteFactory to give ConcreteProduct of say type-X, getInstance() sends back an object of that type. If the client asks for type-Y, then it gets back an object of that type and so on.
  • The object creation method getInstance() of ConcreteFactory is thus standardized and it creates objects of ConcreteProduct set of classes while returning the object in a BaseProduct reference. This is the Factory Method Design Pattern.
Factory Method Design Pattern-Example Use Case in Java-Class Diagram
Factory Method Design Pattern in Java Class Diagram
Code for the classes shown in Java Example's Class Diagram
//Document.java
public interface Document{
  //Specification of methods specific to a Document
  //is done here 
} 
//PDFDocument.java,XMLDocument.java and CSVDocument.java
public class PDFDocument implements Document{
 //Code for overriding Document methods 
}
public class XMLDocument implements Document{
 //Code for overriding Document methods 
}
public class CSVDocument implements Document{
 //Code for overriding Document methods 
}
//DocumentFactory.java
public class DocumentFactory{
  public Document getInstance(String docIdentifier){
    switch(docIdentifier){
      case "XML": return new XMLDocument();
      case "PDF": return new PDFDocument();
      case "CSV": return new CSVDocument();
    }
    return null;
  }  
}
//Client.java
public class Client{
  public static void main(String args[]){
    //creates an instance of XML document
    XMLDocument xmlDocument=(XMLDocument) new DocumentFactory().getInstance("XML");
    //Similarly creates an instance of PDF document
    PDFDocument pdfDocument=(PDFDocument) new DocumentFactory().getInstance("PDF");
  } 
}
Explanation of Code & Class Diagram The Java class diagram above depicts factory pattern implemented for a Document Management System. An important point to note here is that, in practice, in almost all java libraries, the FactoryInterface(as explained in section 'What is Factory Pattern' above) is not created. I.e, The Java implementation directly uses a ConcreteFactory which creates the ConcreteProduct instances.

Lets understand the Java Class Diagram for Factory Method Design Pattern in detail to understand how it works -
  • Interface Document is the base product interface.
  • XMLProduct, CSVProduct and PDFProduct are concrete implementations of Document interface.
  • DocumentFactory has a getInstance() method which takes as input the string docIdentifier. Its through the docIdentifier parameter that the Client conveys the message to DocumentFactory regarding which type of document it needs.
  • DocumentFactory creates the required implementation of Document and returns the object back as type Document. Thus, the getInstance() creates objects of Document’s implementation classes based on client needs. This is how the Factory Method Design Pattern works.
Summary In the above tutorial we looked at what is factory method design pattern moving on to class diagram for a factory method pattern & its explanation. We then looked at class diagram and code for a Java example of factory method pattern followed by its explanation. This concludes the tutorial on factory method design pattern in java.