Factory Method Pattern in Java

April 25, 2014

Factory method is a GoF design pattern which is widely used. The official definition says
The Factory Method Pattern defines an interface for creating an object, but lets the subclasses decide which class to instantiate and Factory Method lets a class defer instantiation to subclasses.

Basically Factory method pattern provides the solution for what the Simple Factory couldn't achieve as  I mentioned in the previous post, ie. fulfilling the Open-Close Principle. This is achieved by providing an interface with a method for creating objects between Client and Concrete Creator classes. This particular method is known as the "factory method". (Here the interface I'm referring to is from a design view point, technically it can be a class or an interface in java)

Simply this pattern allows subclasses to decide which class to instantiate and as in Simple Factory, refers the Concrete Products ie the objects created, though a common interface.

Implementation examples in Java API - java.text.NumberFormatjava.util.Calendar  

The classes which are used to instantiate objects are called creator classes and classes which are used to construct the objects are called product classesTake a look at the following class diagram where there is VehicleFactory which produces Cars and Bikes. (yellow color corresponds to creator classes and green corresponds to product classes)


Let's put the above scenario into code. Car and Bike classes implement Vehicle interface which is referenced by the Client class as the common interface.

Notice that the VehicleFactory class is abstract which contains the abstract method create() [This is the factory method] where as in Simple Factory it was a concrete class. VehicleFactory is used as the base class by the BikeFactory and CarFactory classes to create the concrete products. There can  be other methods implemented which provides default implementations, for instance Calendar.getInstance() method in java.util.Calendar class in Java API. Because of this mechanism, the "fixed" implementation has become a more "flexible" implementation and also loosely coupled. Let's get back to this fact later in the post.



Client receives references of Bike and Car objects through Vehicle interface using BikeFactory and CarFactory.

Let's get back to the flexibility issue (code modification). Assume the situation where the Client needs a Ship rather than a Car or a Bike which is not implemented yet. So all we have to write is a Ship class which implements Vehicle interface and a ShipFactory class implementing VehicleFactory class and that's it, no modification needed to be done to the existing code.

Thank you for reading.

 References     [Head First Design Patterns]
   [http://www.oodesign.com/factory-method-pattern.html]
   [http://courses.caveofprogramming.com/course/java-design-patterns-and-architecture/]
   [Simple Factory vs. Factory Method vs. Abstract Factory]
   [http://stackoverflow.com/questions/4209791/design-patterns]

No comments:

Post a Comment