Abstract Factory Design Pattern
The Abstract Factory Design Pattern is one of the Creational Design Patterns. It provides an interface for creating families of related or dependent objects without specifying their concrete classes.
Unlike the Factory Method Pattern, which creates a single type of object, the Abstract Factory Pattern creates multiple related objects that belong to the same family. It acts as a factory of factories, ensuring that all objects created together are compatible with one another.
This pattern is particularly useful when an application must support multiple product families, such as different operating system themes, database providers, or game environments. By using an Abstract Factory, the client code remains independent of concrete product classes while guaranteeing that related objects are created consistently.
This document presents two practical examples demonstrating the implementation of the Abstract Factory Design Pattern in C++.
Example 1: User Interface Theme Framework
This example demonstrates a graphical user interface framework that supports two visual themes: Light Theme and Dark Theme. Each theme consists of matching buttons and checkboxes.
Program
Explanation
The application works only with the abstract interfaces IButton, ICheckbox, and IUIFactory.
The DarkThemeFactory creates a matching DarkButton and DarkCheckbox, while the LightThemeFactory creates matching light-themed components.
The client never creates concrete objects directly. Instead, it requests related objects through the factory, ensuring that all UI components belong to the same theme.
This demonstrates the Abstract Factory Design Pattern by producing a family of related objects.
Example 2: Strategy Game Faction System
This example demonstrates a real-time strategy game where each faction provides its own compatible soldier and vehicle units.
Program
Explanation
The game uses the IFactionFactory interface to create groups of related objects.
The SciFiFactory creates a CyborgSoldier and a HoverTank, while the SteampunkFactory creates a SteampunkSoldier and a SteampunkZeppelin.
The client never knows which concrete classes are being instantiated. It only communicates with the abstract interfaces.
Because all products come from the same factory, incompatible object combinations cannot occur.
This demonstrates the Abstract Factory Design Pattern by producing compatible families of related objects.
Characteristics of the Abstract Factory Design Pattern
| Property | Description |
|---|---|
| Pattern Type | Creational Design Pattern |
| Core Principle | Create families of related or dependent objects without specifying their concrete classes. |
| Implementation | Uses an abstract factory interface with multiple factory methods that create related products. |
| Main Benefit | Ensures compatibility among related objects while reducing coupling with concrete classes. |
| Difference from Factory Method | Factory Method creates one product, whereas Abstract Factory creates an entire family of related products. |
| Common Applications | GUI frameworks, cross-platform software, game engines, database providers, and theme management systems. |
Conclusion
The Abstract Factory Design Pattern simplifies the creation of related object families by grouping multiple factory methods under a common interface. It allows applications to switch between entire product families without modifying client code while ensuring compatibility between related objects. As demonstrated in the User Interface Theme Framework and Strategy Game Faction System examples, the Abstract Factory pattern improves scalability, consistency, and maintainability in C++ applications.