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)

Simple Factory in Java

April 23, 2014

Factories are used to handle the details of creating objects. Formally put, factories are used to encapsulate object creation. There are 3 variants of implementing factories namely Simple Factory, Factory Method and Abstract Factory.

Although Simple Factory is not an official GoF design pattern it's still worth to know since It'll be useful when understanding Factory Method pattern and Abstract Factory which will be explained in the next post.