Abstract Factory Pattern in C++

Lokesh Bihani
3 min readApr 23, 2024

--

This is the third design pattern of my 8 part design patterns in C++ series. In this article, I’ll introduce you to the Abstract Factory Pattern, which is also a creational design pattern.

In my previous post, I discussed the Factory Method in C++. If you haven’t read it yet, you can check it out here: Factory Method

Abstract Factory

It provides an interface for creating families of related or dependent objects without specifying their concrete classes. It allows clients to create objects of a particular family, without needing to know the actual classes of the objects they are creating.

“Families of related or dependent objects”

The term “family of related or dependent objects” refers to a group of objects that are designed to work together or are dependent on each other in some way. These objects typically share a common theme, purpose, or functionality, and they need to be created in a way that ensures compatibility and consistency among them.

Let’s take an example to illustrate this concept:

Suppose you are developing a GUI toolkit that needs to support different themes, such as a light theme and a dark theme. Each theme consists of various UI components like buttons, checkboxes, and text fields, all designed to visually match the selected theme.

In this scenario:

  • The “family of related or dependent objects” refers to the set of UI components (buttons, checkboxes, text fields) that make up a theme.
  • Each theme (e.g., light theme and dark theme) is a group of related or dependent objects because all the UI components within a theme need to share a consistent visual appearance and behavior.
  • The Abstract Factory pattern provides an interface (the abstract factory) for creating these families of related objects. Each method in the abstract factory corresponds to creating one type of UI component (e.g., createButton(), createCheckbox(), createTextField()).
  • Concrete factory classes (e.g., LightThemeFactory, DarkThemeFactory) implement the abstract factory interface and provide specific implementations for creating UI components that belong to a particular theme.
UML class diagram for Abstract Factory Pattern
// Abstract product interfaces
class Button {
public:
virtual void render() = 0;
};

// Concrete product classes for light theme
class LightButton : public Button {
public:
void render() override {
cout << "Rendering light button" << endl;
}
};

// Concrete product classes for dark theme
class DarkButton : public Button {
public:
void render() override {
cout << "Rendering dark button" << endl;
}
};

class Checkbox {
public:
virtual void render() = 0;
};

class LightCheckbox : public Checkbox {
public:
void render() override {
cout << "Rendering light checkbox" << endl;
}
};


class DarkCheckbox : public Checkbox {
public:
void render() override {
cout << "Rendering dark checkbox" << endl;
}
};
// Abstract factory interface
class ThemeFactory {
public:
virtual Button* createButton() = 0;
virtual Checkbox* createCheckbox() = 0;
};

// Concrete factory for light theme
class LightThemeFactory : public ThemeFactory {
public:
Button* createButton() override {
return new LightButton();
}

Checkbox* createCheckbox() override {
return new LightCheckbox();
}
};

// Concrete factory for dark theme
class DarkThemeFactory : public ThemeFactory {
public:
Button* createButton() override {
return new DarkButton();
}

Checkbox* createCheckbox() override {
return new DarkCheckbox();
}
};
// Client code
int main() {
// Create light theme components
ThemeFactory* lightFactory = new LightThemeFactory();
Button* lightButton = lightFactory->createButton();
Checkbox* lightCheckbox = lightFactory->createCheckbox();
lightButton->render();
lightCheckbox->render();

// Create dark theme components
ThemeFactory* darkFactory = new DarkThemeFactory();
Button* darkButton = darkFactory->createButton();
Checkbox* darkCheckbox = darkFactory->createCheckbox();
darkButton->render();
darkCheckbox->render();

// Clean up
delete lightButton;
delete lightCheckbox;
delete darkButton;
delete darkCheckbox;
delete lightFactory;
delete darkFactory;

return 0;
}

Difference from Factory Method Pattern

The main difference between the Abstract Factory Pattern and the Factory Method Pattern is that the Abstract Factory is about creating families of related or dependent products, while the Factory Method is used for creating one product only.

In other words, the Abstract Factory is a generalization of multiple products that belong to a family, while the Factory Method is a specialization of a single product.

Summary

  • Abstract Factory Pattern is useful for creating families of related objects in a flexible and maintainable way.
  • By providing an interface for creating objects without specifying their concrete classes, it promotes code reusability and simplifies the process of adding new types of objects to an application.

--

--

Lokesh Bihani

Software Engineer passionate about System Design, DevOps, and ML. I try to simplify complex tech concepts to help others learn while deepening my own knowledge.