Abstract Factory (aka Kit)
Intent
An interface for creating families of related or dependent objects without specifying their concrete classes.
A design that provides a way to create families of related objects without imposing their concrete classes, by encapsulating a group of individual factories that have a common theme without specifying their concrete classes.
Problem
How can an application be independent of how its objects are created?
How can a class be independent of how the objects that it requires are created?
How can families of related or dependent objects be created?
Solution
Encapsulate object creation in a separate (factory) object by defining and implementing an interface for creating objects.
Delegate object creation to a factory object instead of creating objects directly.
Advantages
✅ You avoid tight coupling between concrete products and client code. ✅ Single Responsibility Principle. You can extract the product creation code into one place, making the code easier to support. ✅ Open/Closed Principle. You can introduce new variants of products without breaking existing client code.
Disadvantages
❌ The code may become more complicated than it should be, since a lot of new interfaces and classes are introduced along with the pattern.
Note
💡 A more recent structure of the pattern is based on interfaces that define the abstract factory and the abstract products to be created. This design uses native support for interfaces or protocols in mainstream programming languages to avoid inheritance.
Last updated