Intent - Provide a contract for creating families
of related or dependent objects without having to specify
their concrete classes
Problem -
You need to create products, but need the system to
be independent of how the products are created
Your system needs to be configured with one of
multiple families of products
A family of products needs to be used together, and
you need to enforce this constraint
You need to hide the implementation of a library of
products by just revealing their interface.
Solution - Coordinate the creation of families of
objects. The rules of how to instantiate the products is
implemented in the factory, taking the responsibility from
the client.
Participants and Collaborators
AbstractFactory
ConcreteFactory
AbstractProduct
ConcreteProduct
Client
Consequences
Isolates concrete classes
Makes exchanging product families easy
Promotes consistency among products
Supporting new kinds of products can be difficult
Implementation
Abstract Factories are often Singletons
Often, a Factory Method is used to create the
product
The Abstract Factory is just used to create products. But unlike
the simple Factory, it defers the instantiation to subclasses. This
allows the products to be hidden from the client (just written to an
interface)