Introducing the Abstract Factory design pattern

Introducing the Abstract Factory design pattern

Read Time : 3 Minutes

Friday, 07 April 2023

Design patterns are proven solutions to software design problems. They help improve code quality, promote reusability, and increase maintainability. We use them to save time and produce quality, extensible and flexible code. In this article, we are going to introduce the Abstract Factory design pattern.

The Abstract Factory pattern is a creational design pattern that allows the creation of a collection of related or dependent objects without specifying their class types. In fact, this pattern defines abstract rules for the production of objects and is implemented according to the need in different situations.

The Abstract Factory pattern consists of several key components:

  • Abstract Factory: An interface that defines methods for creating a set of related objects. This interface does not include any details in creating objects and only contains abstractions.
  • Concrete Factory: A class that implements the Abstract Factory interface and creates a collection of related objects that work together. Each Concrete Factory is responsible for creating objects for a specific need.
  • Abstract Product: An interface that defines the general and abstract format of the objects that are to be produced. The type of objects created by Abstract Factory is of this type.
  • Concrete Product: A class that implements the Abstract Product interface and defines the implementation details for each object.

The main advantage of the Abstract Factory pattern is that it provides a way to encapsulate the creation of related objects and does not involve the user in the details of object creation. In addition, in case of changes in the implementation, the user codes will not need to be changed.

In the following, we will understand this pattern better with an example:

 

// Abstract Product A
public interface IShape
{
    void Draw();
}
// Concrete Product A1
public class Circle : IShape
{
    public void Draw()
    {
        Console.WriteLine("Drawing a Circle");
    }
}
// Concrete Product A2
public class Square : IShape
{
    public void Draw()
    {
        Console.WriteLine("Drawing a Square");
    }
}
// Abstract Product B
public interface IColor
{
    void Fill();
}
// Concrete Product B1
public class Red : IColor
{
    public void Fill()
    {
        Console.WriteLine("Filling with Red");
    }
}
// Concrete Product B2
public class Blue : IColor
{
    public void Fill()
    {
        Console.WriteLine("Filling with Blue");
    }
}
// Abstract Factory
public interface IShapeAndColorFactory
{
    IShape CreateShape();
    IColor CreateColor();
}
// Concrete Factory 1
public class CircleAndRedFactory : IShapeAndColorFactory
{
    public IShape CreateShape()
    {
        return new Circle();
    }
    public IColor CreateColor()
    {
        return new Red();
    }
}
// Concrete Factory 2
public class SquareAndBlueFactory : IShapeAndColorFactory
{
    public IShape CreateShape()
    {
        return new Square();
    }
    public IColor CreateColor()
    {
        return new Blue();
    }
}

In this example, we have two Abstract Products named IShape and IColor and their implementation in the form of Circle, Square, Red and Blue. Then we have created an Abstract Factory called IShapeAndColorFactory. Next, we have implemented IshapeAndColorFactory with two modes CircleAndRedFactory and SquareAndBlueFactory.

The IShapeAndColorFactory interface defines methods to create an IShape and an IColor, and the classes that implement it each provide their own methods to create these objects.

 

To read about other design patterns, you can use the list below. There is also a code repository on GitHub that includes all the design patterns.

design patterns

Creational:Factory Design Pattern
Builder Design Pattern
Singleton Design Pattern
Prototype Design Pattern
Abstract Factory Design Pattern
Structural:Adapter Design Pattern
Bridge Design Pattern
Composite Design Pattern
Decorator Design Pattern
Facade Design Pattern
Flyweight Design Pattern
Proxy Design Pattern
Behavioral:Chain of Responsibility Design Pattern
Command Design Pattern
Interpreter Design Pattern
Mediator Design Pattern
Memento Design Pattern
Observer Design Pattern
State Design Pattern
Strategy Design Pattern
Template Method Design Pattern
Visitor Design Pattern
Iterator Design Pattern
  • Share:
reza babakhani
Reza Babakhani

I am Reza Babakhani, a software developer. Here I write my experiences, opinions and suggestions about technology. I hope that what I write is useful for you.

Latest post

Service Mesh Simplifying Microservice Communication

In the ever-evolving landscape of software development, microservices architecture has gained considerable popularity due to its scalability, flexibility, and extensibility. However, as the number of microservices in an application increases, it becomes increasingly challenging to manage their communication and ensure that they are all properly visible. This is where Service Mesh comes into play.

The Importance of Edge Computing

Due to the speed of technological evolution, one of the concepts that has attracted a lot of attention and changes the way we interact with digital systems is edge computing.

What is event-driven architecture?

Event-driven architecture (EDA) is a software design pattern that has become increasingly popular in modern software development. In this architecture, the flow of data is determined by the occurrence of events. Unlike traditional centralized systems that are constantly checking for new status. Event-driven architecture is especially useful for systems that need to process large amounts of data in real-time.

leave a comment