codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎭

Design Patterns

8 / 100 topics
5Introduction to Creational Patterns6Singleton Pattern7Factory Method Pattern8Abstract Factory Pattern9Builder Pattern10Prototype Pattern31Practical Exercises for Creational Patterns
Tutorials/Design Patterns/Abstract Factory Pattern
🎭Design Patterns

Abstract Factory Pattern

Updated 2026-05-15
10 min read

Abstract Factory Pattern

Introduction

The Abstract Factory pattern is one of the creational design patterns that provides an interface for creating families of related or dependent objects without specifying their concrete classes. This pattern is particularly useful when a system should be independent of how its products are created, composed, and represented.

In this tutorial, we will explore the Abstract Factory pattern in detail, understand its components, and see practical examples to illustrate how it can be implemented in real-world scenarios.

Concept

The Abstract Factory pattern involves creating an interface for creating families of related or dependent objects without specifying their concrete classes. This is achieved through the following key components:

  1. AbstractFactory: Declares a set of methods for creating abstract products.
  2. ConcreteFactory: Implements the operations to create concrete product objects.
  3. AbstractProduct: Declares an interface for a type of product object.
  4. ConcreteProduct: Defines a product object to be created by the corresponding concrete factory.
  5. Client: Uses interfaces declared by AbstractFactory and AbstractProduct classes.

The main advantage of using the Abstract Factory pattern is that it allows you to encapsulate a group of individual factories, without specifying their concrete classes. This makes the system more flexible and easier to extend.

Examples

Let's walk through an example to understand how the Abstract Factory pattern works in practice. We'll create a simple application that simulates different types of UI components for both Windows and Mac operating systems.

Step 1: Define the Abstract Products

First, we define abstract products for buttons and checkboxes:

JavaScript
1// AbstractProductA
2class Button {
3 render() {}
4}
5
6// AbstractProductB
7class Checkbox {
8 render() {}
9}

Step 2: Create Concrete Products

Next, we create concrete implementations of these products for Windows and Mac operating systems:

JavaScript
1// ConcreteProductA1
2class WinButton extends Button {
3 render() {
4 console.log('Rendering a Windows button');
5 }
6}
7
8// ConcreteProductB1
9class WinCheckbox extends Checkbox {
10 render() {
11 console.log('Rendering a Windows checkbox');
12 }
13}
14
15// ConcreteProductA2
16class MacButton extends Button {
17 render() {
18 console.log('Rendering a Mac button');
19 }
20}
21
22// ConcreteProductB2
23class MacCheckbox extends Checkbox {
24 render() {
25 console.log('Rendering a Mac checkbox');
26 }
27}

Step 3: Define the Abstract Factory

Now, we define an abstract factory that declares methods for creating these products:

JavaScript
1// AbstractFactory
2class GUIFactory {
3 createButton() {}
4 createCheckbox() {}
5}

Step 4: Create Concrete Factories

We then create concrete factories for Windows and Mac operating systems:

JavaScript
1// ConcreteFactory1
2class WinFactory extends GUIFactory {
3 createButton() {
4 return new WinButton();
5 }
6 createCheckbox() {
7 return new WinCheckbox();
8 }
9}
10
11// ConcreteFactory2
12class MacFactory extends GUIFactory {
13 createButton() {
14 return new MacButton();
15 }
16 createCheckbox() {
17 return new MacCheckbox();
18 }
19}

Step 5: Implement the Client

Finally, we implement a client that uses these factories to create and render UI components:

JavaScript
1// Client
2function Application(factory) {
3 const button = factory.createButton();
4 const checkbox = factory.createCheckbox();
5
6 button.render();
7 checkbox.render();
8}
9
10const winFactory = new WinFactory();
11const macFactory = new MacFactory();
12
13console.log('Creating Windows UI components:');
14Application(winFactory);
15
16console.log('
17Creating Mac UI components:');
18Application(macFactory);

Output

When you run the above code, you will see the following output:

Output
Creating Windows UI components:
Rendering a Windows button
Rendering a Windows checkbox

Creating Mac UI components:
Rendering a Mac button
Rendering a Mac checkbox

What's Next?

In this tutorial, we explored the Abstract Factory pattern and how it can be used to create families of related or dependent objects without specifying their concrete classes. In the next section, we will delve into another creational design pattern called the Builder Pattern, which is useful for constructing complex objects step by step.

Stay tuned for more insights into design patterns and how they can help you build robust and maintainable software systems!


PreviousFactory Method PatternNext Builder Pattern

Recommended Gear

Factory Method PatternBuilder Pattern