Design pattern Abstract Factory, in a nutshell
This pattern works together with the factory method, my last article before it, and creates a family of objects. Based on that, let’s see your structure and how to implement it.
What did I use as references?
To write this article I used the Gang Of Four, Head First Design Patterns, and Wikipedia. All the links are available in the References section.
Motivation
Consider a user interface toolkit that supports multiple look-and-feel standards, such as Motif and Presentation Manager. Different look-and-feels define different appearances and behaviors for user interface “widgets” like scroll bars, windows, and buttons. To be portable across look-and-feel standards, an application should not hard-code its widgets for a particular look and feel. Instantiating look-and-feel-specific classes of widgets throughout the application makes it hard to change the look and feel later.[1]
We can solve this problem by defining an abstract WidgetFactory class that declares an interface for creating each basic kind of widget. There’s also an abstract class for each kind of widget, and concrete subclasses implement widgets for specific look-and-feel standards. WidgetFactory’s interface has an operation that returns a new widget object for each abstract widget class. Clients call these operations to obtain widget instances, but clients aren’t aware of the concrete classes they’re using. Thus clients stay independent of the prevailing look and feel.[1]
Where can I use this pattern?
A system should be independent of how its products are created, composed, and represented. [1]
A system should be configured with one of multiple families of products.[1]
A family of related product objects is designed to be used together, and you need to enforce this constraint.[1]
You want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.[1]
Definition
In order to recognised an Abstract Factory you must have the following classes [1], as the GOF book says.
- AbstractFactory → Declares an interface for operations that create abstract product objects.
- ConcreteFactory → Implements the operations to create concrete product objects.
- AbstractProduct → Declares an interface for a type of product object.
- ConcreteProduct → Defines a product object to be created by the corresponding concrete factory.
- Client → Uses only interfaces declared by AbstractFactory and AbstractProduct classes.
Examples
GOF
How this pattern is represented on GOF book.
Hands on
The hands on will be coded using the following diagram below and respecting the Abstract Factory class set.
For this example I’m assuming the following model. The client has the option to choose between Windows and MacOS, and based on choice the features for each system are created.
If you prefer, access the diagram using this link.
Let’s see how the code looks like 🖥 🖱
Abstract Factory
Concrete Factories
Abstract Products
Concrete Products
Concrete Products that implements MicrosoftOffice
Concrete Products that implements IWork
Computer
This class serve to receive a computer created, having all abstract products and the method print, print all objects. Apart from it, this class also use the Build pattern, for more details click here.
Client
This Client class, iterate over the ComputerType enum and create a new computer for each item. Besides the method getInstance use the factory method, explained on my previous article.
Conclusion
This patterns is very useful to create a family of objects and avoid a bunch of code smell or bad practices when the target is create a set of objects. Besides is easy to other person recognised the abstract factory using the structure Abstract Factory, Abstract Product, Concrete Product, and Client.
Code
The complete code is available on my GitHub.
References
1 — Gamma Erich, Helm Richard, Johnson Ralph, Vlissides John, Grady Booch. (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley.
2 — Eric Freeman, Elisabeth Robson. (2020). Head First Design Patterns, 2nd Edition. O’Reilly Media, Inc.
3 — First image