Tag Archives: Design Patterns

Abstract Factory vs. Factory Method Design Pattern

Differences between abstract factory pattern and factory method pattern .

Both Abstract Factory and Factory design pattern are creational design pattern and they are used to decouple clients from creating object they need, But there is a significant difference between Factory and Abstract Factory design pattern, Factory design pattern produces implementation of Products e.g. Garment Factory produce different kinds of clothes, On the other hand Abstract Factory design pattern adds another layer of abstraction over Factory Pattern and Abstract Factory implementation itself e.g. Abstract Factory will allow you to choose a particular Factory implementation based upon need which will then produce different kinds of products. In short Abstract Factory design pattern creates Factory and Factory design pattern creates Products.

Difference between Abstract Factory and Factory Method design patterns in Java in point form:

1) One more difference between Abstract Factory and Factory Method design patterns is that Abstract Factory pattern uses composition to delegate responsibility of creating object to another class while Factory design pattern uses inheritance and relies on derived class or sub class to create object.

2) Abstract Factory may use Factory design pattern for creating objects but they are not just limited to that they can also use Builder design pattern to build object by doing series of steps or Prototype pattern to build object by copying or customizing prototype of that object. It completely depends upon your implementation whether to use Factory pattern or Builder pattern for creating products.

3) Factory method design pattern are modern way of creating objects. It offers some notable advantages over new() operator to create Objects e.g. By using Factory method design pattern client is completely decoupled with object creation code, which enforces Encapsulation and result is loosely coupled and highly cohesive system. Any change e.g. a new product from Factory requires almost no change in existing clients. On the other hand if we need an additional level of abstraction over your Factory pattern than Abstract Factory is the right design pattern to use. Abstract Factory allows us to use different Factory implementation for different purpose. Abstract Factory pattern can be implemented using Factory method and Singleton design pattern in Java. One of the best example of Abstract Factory and Factory pattern In Java is DocumentBuilderFactory and DocumentBuilder javax.xml.parsers package.

In short Abstract Factory design pattern provides abstraction over Factory Method pattern itself while Factory Method design pattern provides abstraction over products[1].

References: 1. http://javarevisited.blogspot.com/2013/01/difference-between-factory-and-abstract-factory-design-pattern-java.html