Abstract Factory#

tl;dr - A factory of factories; a factory that groups the individual but related/dependent factories together without specifying their concrete classes.

An abstract factory is a design pattern used in object-oriented programming that provides an interface for creating families of related or dependent objects without specifying their concrete classes. It falls under the category of creational design patterns.

Here’s a breakdown of the key components and concepts involved in the abstract factory pattern:

  1. Abstract Factory Interface:
    • Defines the interface for creating a family of related or dependent objects.

    • Declares a set of abstract methods, each responsible for creating a different type of object.

  2. Concrete Factories:
    • Implement the abstract factory interface to produce a family of related objects.

    • Each concrete factory corresponds to a specific variant or family of objects.

  3. Abstract Products:
    • Define the interface for a group of related or dependent products.

    • The abstract product classes declare the methods that concrete product classes must implement.

  4. Concrete Products:
    • Implement the abstract product interface.

    • Each concrete product corresponds to a specific variant or family of products.

  5. Client:
    • Utilizes the abstract factory and abstract product interfaces to create families of related objects.

    • Remains unaware of the specific classes of objects it creates.

The main idea is to encapsulate the creation of objects in a separate hierarchy (the abstract factory) and allow the client code to work with this hierarchy without specifying concrete classes. This provides flexibility, as you can easily switch between different families of objects by using a different concrete factory.

The abstract factory pattern is similar to the factory method pattern. The key difference is that the factory method pattern is a single method, whereas the abstract factory pattern is an object. The factory method is just a method, whereas the abstract factory is an object that has multiple factory methods on it.

--- UML diagram ---
--- Python Example ---
--- Java Example ---