About 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.

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.

Related Whitepaper:

Bulletproof Java Code: A Practical Strategy for Developing Functional, Reliable, and Secure Java Code

Use Java? If you do, you know that Java software can be used to drive application logic of Web services or Web applications. Perhaps you use it for desktop applications? Or, embedded devices? Whatever your use of Java code, functional errors are the enemy!

To combat this enemy, your team might already perform functional testing. Even so, you're taking significant risks if you have not yet implemented a comprehensive team-wide quality management strategy. Such a strategy alleviates reliability, security, and performance problems to ensure that your code is free of functionality errors.Read this article to learn about this simple four-step strategy that is proven to make Java code more reliable, more secure, and easier to maintain.

Get it Now!  

One Response to "Abstract Factory Design Pattern Explained"

  1. Venkat says:

    Excellent Explanation…Thank you very much for the post…

Leave a Reply


five + 2 =



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close