Abstract Factory Pattern

Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. Given a set of related abstract classes, the Abstract Factory pattern provides a way to create instances of those abstract classes from a matched set of concrete subclasses. Following figure illustrates the Abstract Factory pattern.The Abstract Factory pattern provides an abstract class that determines the appropriate concrete class to instantiate a set of concrete products that implement a standard interface. The client interacts only with the product interfaces and the Abstract Factory class. The client never knows about the concrete construction classes provided by this pattern. The Abstract Factory pattern is similar to the Factory Method pattern, except it creates families of related objects.

UML Representation


The following lists the benefits of using the Abstract Factory pattern:
  • Isolates the concrete classes from client
  • Allows for exchanging product families easy
  • Promotes consistency among products by implementing the common interface

When to Use

You should use the Abstract Factory pattern when:
  • The system should be independent of how its products are created, composed, and represented.
  • The system should be configured with one of multiple families of products—for example, Microsoft Windows or Apple OSX classes.
  • The family of related product objects is designed to be used together, and you must enforce this constraint. This is the key point of the pattern; otherwise, you could use a Factory Method.
  • You want to provide a class library of products, and reveal only their interfaces, not their implementations.