Core Java

Abstract Factory Design Pattern Explained

Abstract Factory Design Pattern is another flavor of Factory Design Pattern. This pattern can be considered as a “super factory” or “Factory of factories”.

The Abstract Factory design pattern (part of the Gang of Four) falls under the Creational design pattern category and it provides a way to encapsulate a group of factories that have a common link without highlighting their concrete classes. This is all about a factory creating various objects at run time based on user demand. The client remains completely unaware (decoupled) on which concrete products it gets from each of these individual factories and the client is only able to access a simplified interface.

Definition:
Abstract Factory Design Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.

Problem Statement:

We will consider our same previous example of Garment Factory and extend it to understand the problem statement for Abstract factory. Consider a garments factory specialized in creating trousers and shirts. Now the Parent company which is a famous Retail brand is now venturing into Gadget section. They are also planning to expand their Factories having one centre in US and another one in UK. The client should be completely unaware of how the objects are created. What is the best design pattern we can use to resolve this requirement?

Solution:

To solve the above design problem we will use Abstract Factory Pattern. As mentioned earlier this is the super Factory. The above problem cannot be resolved efficiently using the Factory method pattern as this involves multiple Factories and products which are related to the parent company or dependent.
Note: In design pattern abstract and interface can be referred by the same name.

Structure:

Abstract Factory Design Pattern

In the above diagram the additional items created are the additional layer of abstraction through AbstractFactory having the createProductA() and createProductB() method. There are multiple ConcreteFactories which can implement the methods of the AbstractFactory. The client now accesses only the AbstractFactory interface.

The other part is the Product. The client now accesses to different AbstractProduct interfaces AbstractProductA and AbstractProductB. All the ConcreteProducts for AbstractProducts are created by the ConcreteFactories (ConcreteFactory1 and ConcreteFactory2) and it’s logic.

Now let’s have a look at our real life GarmentFactory example and what’s the difference than the Factory Method pattern.

Abstract Factory Example

In the above real life example the RetailFactory is AbstractFactory class which now has multiple Concrete factories (UKFactory and USFactory) in various locations like US and UK specialized in creating multiple products like Shirt/Laptop and Trouser/Mobile respectively. In this example we have also created another additional class called FactoryMaker which takes the choice of Factory from the client and then delegates the job to appropriate Factory classes accordingly. The client is completely unaware of how this processing is done and has reference to only the RetailFactory interface and GarmentType and GadgetType interface. This loose coupling also helps in terms of addition of multiple Concrete Products without much change in the client code.

Benefits:

Use of this pattern makes it possible to interchange the concrete classes without changing the client code even at runtime.

Drawback:

One of the main drawbacks is the extra complexity and writing the code during the initial stages.

Do you know?
Data Access Object in JEE uses the (GoF) Abstract Factory Pattern to create various product DAO from RdbDAOFactory, XmlDAOFactory, OdbDAOFactory.

 
Interesting points:

  • Abstract Factory, Builder, and Prototype can use Singleton in their implementation. Abstract Factory Pattern is often used along with Factory Method but also can be implemented using Prototype pattern to increase the performance and simplifying the code.
  • Abstract Factory can be used as an alternative to Façade pattern to hide platform specific classes
  • AbstractFactory class declares only an interface for creating the products. The actual creation is the task of the ConcreteProduct classes, where a good approach is applying the Factory Method design pattern for each product of the family.

Difference between Abstract Factory and Factory Method pattern:

  • Factory Method pattern exposes a method to the client for creating the object whereas in case of Abstract Factory they expose a family of related objects which may consist of these Factory methods.
  • Designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.
  • Factory Method pattern hides the construction of single object where as abstract factory method hides the construction of a family of related objects. Abstract factories are usually implemented using (a set of) factory methods.

 
Reference: Abstract Factory Design Pattern Explained from our JCG partner Mainak Goswami at the Idiotechie blog.

Mainak Goswami

Mainak Goswami is an experienced Technology Consultant specializing in JEE, Web Development and Open source technologies. He is currently based out of United Kingdom. He is a technology enthusiast trying to explore the latest in the world of technology. His current area of interest is Mobility, NoSQL and Cloud computing. In past time he loves blogging on his website Idiotechie.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Venkat
Venkat
11 years ago

Excellent Explanation…Thank you very much for the post…

Back to top button