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.
|Abstract Factory Design Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.|
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?
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.
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.
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.
Use of this pattern makes it possible to interchange the concrete classes without changing the client code even at runtime.
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.|
- 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.