The Abstract Factory Design Pattern

The Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.

Use the Abstract Factory pattern when you have families of related objects that need to be created together. It is particularly useful when client code should be independent of the system it is composing.

  • TypeCreational
  • Time ComplexityO(1) Constant Time – The algorithm runs in constant time, irrespective of the input size. Operations like accessing an array element fall into this category.
  • Efficiency6
  • Learning Effort

    • Average6
    • Conceptual7
    • Debug and Maintain5
    • Implementation6
    • Prerequisites6
    • Versatility8

Things you should know before you start

  • Interfaces Understanding of interfaces in object-oriented programming.
  • Inheritance Understanding of inheritance and how to use subclassing.

Minimal Example

Creating different UI elements that are consistent within multiple themes in a graphical application.

interface UIFactory {
  createButton(): Button;
  createCheckbox(): Checkbox;
}
class DarkThemeButton implements Button {
  render() {
    console.log('Rendered a dark theme button.');
  }
}
const uiFactory: UIFactory = new DarkThemeUIFactory();
const button = uiFactory.createButton();
button.render();

What’s happening in thie example

In this example, the DarkThemeUIFactory is a ConcreteFactory that produces UI elements of the DarkTheme family. The `clientCode` uses this factory to create a DarkThemeButton and render it.

How the Abstract Factory Works

The Abstract Factory pattern involves multiple Factory Methods, each for creating different types of objects. These objects typically belong to a related family.

It isolates the client code from concrete classes. The client interacts only with the abstract classes, so it can work with any class implementations that follow the factory interface.

Entities in the Abstract Factory Design Pattern

  • AbstractFactoryThe AbstractFactory class declares a set of methods for creating different types of products.

    • AbstractFactory has a one-to-many instantiates relationship with AbstractProduct
  • AbstractProductThe AbstractProduct class declares the interface for types of products that the factory will create.

    • ConcreteProduct has a one-to-one implements relationship with AbstractProduct

What to watch out for when using or considering this design pattern.

Avoid using the Abstract Factory pattern when the families of related objects you’re creating either do not need to be distinct, or you want to allow mixing of families. It can add unnecessary complexity to the code.

Design patterns often confused with the Abstract Factory Design Pattern

The following design patterns are often confused with the Abstract Factory
for various resons.

If you’re exploring solutions, have a look through this list and see if
one of these might be a better fit for your problem.

  • Factory Method – The Factory Method pattern provides an interface for creating instances of a class, with its subclass deciding which class to instantiate. It focuses on single object creation.

    The Factory Method pattern is similar to the Abstract Factory in the following ways.

    • Both are creational design patterns.
    • Both encapsulate object creation.
  • Here are some notable differences with the Abstract Factory pattern.

    • Abstract Factory creates families of related objects, while Factory Method focuses on the instantiation of a single object.
    • Abstract Factory involves multiple Factory Methods for creating different types of objects, while Factory Method usually involves a single method.
  • Singleton – The Singleton pattern ensures that a class has only one instance and provides a global point to access it.

    The Singleton pattern is similar to the Abstract Factory in the following ways.

    • Both are creational design patterns.
    • Both aim to control object creation.
  • Here are some notable differences with the Abstract Factory pattern.

    • Singleton restricts object creation to one instance, while Abstract Factory focuses on creating families of related objects.
    • Abstract Factory is more complex and versatile, enabling the creation of multiple types of related objects, whereas Singleton restricts to a single object instance.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top