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:
Explanation of the code
BaseProductis the base interfaces for all products.
ConcreteProductimplements BaseProduct. There are usually multiple
ConcreteProductimplementations for a
BaseProductwhen a factory design pattern is chosen for object creation.
FactoryInterfacedefines the basic behavior of the factory which in this case is defined by the method
FactoryInterfaceand provides implementation for the
getInstance()method. There can be mutiple
getInstance()method creates an object of one of the
ConcreteProductclasses. It however sends back an object of type
- The actual object created is as per the client invocation context. I.e. if the client asks
ConcreteProductof 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
ConcreteFactoryis thus standardized and it creates objects of
ConcreteProductset of classes while returning the object in a
BaseProductreference. This is the Factory Method Design Pattern.
Factory Method Design Pattern-Example Use Case in Java-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
ConcreteFactorywhich creates the
Lets understand the Java Class Diagram for Factory Method Design Pattern in detail to understand how it works –
Documentis the base product interface.
PDFProductare concrete implementations of
getInstance()method which takes as input the string
docIdentifier. Its through the
docIdentifierparameter that the
Clientconveys the message to
DocumentFactoryregarding which type of document it needs.
DocumentFactorycreates the required implementation of
Documentand 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.
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.