Abstract Factory is a creational design pattern published by the Gang of Four. Its intent is to, "Provide an interface for creating families of related or dependent objects without specifying their concrete classes." (page 87) Note that [factory-method] is a separate GoF pattern, and there are additional [factory] patterns outside the GoF.
The Abstract Factory Pattern enforces a theme across a group of related objects. The canonical example from the GoF book is a look-and-feel standard enforced across a group of GUI widgets. It is implemented by five participants. (page 89)
- AbstractFactory (e.g. WidgetFactory) - declares an interface for operations that create
AbstractProduct
objects.- ConcreteFactory (e.g. MotifWidgetFactory, PMWidgetFactory) - implements the operations to create
ConcreteProduct
objects.- AbstractProduct (e.g. Window, ScrollBar) - declares an interface for a type of product object.
- ConcreteProduct (e.g. MotifWindow, MotifScrollBar) - defines a product object to be created by the corresponding
ConcreteFactory
and implements theAbstractProduct
interface.- Client - uses only interfaces declared by
AbstractFactory
andAbstractProduct
classes.
The Gang of Four offer the following criteria for applying the Abstract Factory Pattern. (page 88)
Use the Abstract Factory pattern when
- a system should be independent of how its products are created, composed, and represented.
- a system should be configured with one of multiple families of products.
- a family of related product objects is designed to be used together, and you need to enforce this constraint.
- you want to provide a class library of products, and you want to reveal just their interfaces, not their implementations.
The pattern has the following consequences. (page 89)
- It isolates concrete classes.
- It makes exchanging product families easy.
- It promotes consistency among products.
- Supporting new kinds of products is difficult.
See Abstract Factory for a longer summary of the GoF book.
Note the Gang of Four published two different factory patterns, the other being factory-method. Additionally, there are factory patterns outside the GoF book, so the term "factory" by itself is ambiguous.