Abstract Factory Pattern

Summary

  • The Abstract Factory pattern supports the creation of products that exist in families and are designed to be produced together, while keeping their details independent from the clients.
  • The Abstract Factory pattern hides the factory name from the client, which makes it possible to swap the factory, while still providing the functionality according to an agreed interface.
  • A limitation of the Abstract Factory pattern is that it is not easy to add new kinds of products. The number and names of the abstract products are coded directly into the abstract factory therefore, adding a new product requires the factory interface and all its sub-classed concrete factories to be updated. Even the client needs to change to take advantage of the new addition.

Class diagram: (see Abstract Factory Pattern code review)

Abstract Factory Pattern class diagram of sample code download

Usage

Use the Abstract Factory pattern when:

  • A system should be independent of how its products are created, composed, and represented.
  • A system can be configured with one of multiple families of products.
  • The constraint requiring products from the same factory to be used together must be enforced.
  • The emphasis is on revealing interfaces, not implementations.