Wednesday, July 26, 2023

Difference between Factory and Abstract Factory Design Pattern in Java? Example

Both the Abstract Factory and Factory design pattern are creational design pattern and use to decouple clients from creating objects they need, But there is a significant difference between Factory and Abstract Factory design patterns, Factory design pattern produces implementation of Products like 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 like the AbstractFactory will allow you to choose a particular Factory implementation based upon need which will then produce different kinds of products.

In short
1) Abstract Factory design pattern  creates Factory
2) Factory design pattern creates Products




Difference between Abstract Factory and Factory design pattern in Java

Let see another example of Abstract Factory and Factory design pattern in Java from JDK itself to get a better understanding. If you have done some XML work in Java e.g. reading XML files using DOM parser, you may be familiar with DocumentBuilderFactory  class which is an example abstract factory design pattern because it returns a factory called DocumentBuilder which then used to create Document.



//Example of Abstract Factory and Factory design pattern  in Java
DocumentBuilderFactory abstractFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder factory = abstractFactory.newDocumentBuilder();
Document doc = factory.parse(stocks)

In this example DocumentBuilderFactory (Abstract Factory) creates DocumentBuilder (Factory) which creates Documents (Products).

Let's see some more difference between Abstract Factory and Factory design pattern in Java in point form :

1) One more difference between Abstract Factory and Factory design pattern is that AbstractFactory 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 the prototype of that object. It completely depends upon your implementation whether to use Factory pattern or Builder pattern for creating products.

Here is also a nice diagram which clearly shows the difference between factory and abstract factory design pattern for easier understanding:

Difference between Factory and Abstract Factory Design Pattern in Java? Example



When to use Abstract Factory and Factory method design pattern in Java

Difference between Factory vs Abstract Factory pattern in JavaFactory method design patterns are a modern way of creating objects. It offers some notable advantages over new() operator to create Objects e.g. By using the Factory method design pattern client is completely decoupled with object creation code, which enforces Encapsulation, and the result is a loosely coupled and highly cohesive system. 

Any change like a new product from Factory requires almost no change in existing clients. See When to use Factory method design pattern in Java for more scenarios. 

On the other hand if you need an additional level of abstraction over your Factory pattern then Abstract Factory is the right design pattern to use. Abstract Factory allows you to use different Factory implementation for different purposes. 

Abstract Factory pattern can be implemented using Factory method and Singleton design pattern in Java. One of the best examples of Abstract Factory and Factory patterns in Java is DocumentBuilderFactory and DocumentBuilder javax.xml.parsers package.


That's all on the difference between Abstract Factory and Factory design pattern in Java. In short Abstract Factory, design pattern provides an abstraction over Factory pattern itself while Factory design pattern provides an abstraction over products.


Other Java design pattern tutorials from the Javarevisited blog

5 comments :

Arun Viswanath said...

Can you please explain more with some example on the following difference you mentioned:

"One more difference between Abstract Factory and Factory design pattern is that AbstractFactory 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."

Sasi kumar said...

nice explanation.. SessionFactory is AbstractDesign pattern, and session is Factory design pattern, am i right.

Anonymous said...

I think that the approach is wrong. From what I understand, AbstractFactory is not a class that creates factories. Instead, it is a different class that creates families of related products.

With factory method, our class itself or its sublasses create the instance of the product.

With abstract factory, our class delegates the creation to another class or to subclasses of that class. This other class (or its subclasses) is the one that creates the object we need. It may also create some other related products as well (a family of products). Each product is created with its own kind of factory method.

See http://stackoverflow.com/questions/5739611/differences-between-abstract-factory-pattern-and-factory-method

javin paul said...

@Anonymous, AbstractFactory as name state itself is to abstract Factory class, which is responsible for creating products e.g. A factory for creating Canadian Pizza could be different than factory for creating American or Italian Pizza. You can abstract this detail by using AbstractFactory pattern and depending upon whether your application is running it can pick corresponding implementation of abstract factory.

Anonymous said...

Abstract Factory is like a CarFactory, the class implementing the CarFactory decides whether it's IndianCar, GermanCar or AmericanCar.

Post a Comment