Abstract Factory Design Pattern in C#

In  this article, I will explain about how to implement abstract factory design pattern in C#. Basic understanding of factory pattern will be helpful to understand this article.

Abstract factory design pattern comes under creational design pattern category of Gang of Four (GoF) design patterns. Abstract factory pattern is effective to create set of objects that are somehow related or inter-dependent.

What is Abstract Factory Design Pattern?

Abstract factory design pattern provides an interface to create the set of related or dependent objects without specifying their concrete classes. Abstract factory pattern behaves as the master factory which is responsible for creating other factories.

Within abstract factory, factory design pattern is used to create objects. However, it is not mandatory to use factory pattern within abstract factory. Depending upon your requirement or choice, you can also use builder pattern or prototype pattern to create objects within abstract factory.

Gang of Four (GoF) Definition:
“Provide an interface for creating families of related or dependent objects without specifying their concrete classes.”

Abstract Factory Design Pattern – UML Diagram

Abstract Factory design pattern in C# - UML diagram

Participants are:

  • AbstractFactory: This is an Interface that is used to create abstract products
  • ConcreteFactory: Implements AbstractFactory interface to create concrete product objects
  • AbstractProduct: This is an Interface which declares type of Product object.
  • Product: Implements AbstractProduct interface to create Product object.
  • Client: Uses AbstractFactory and AbstractProduct interfaces to create family of related objects

Abstract Factory Design Pattern in C# – Example

To understand abstract factory design pattern in C#, lets implement an example.

Problem statement

Consider a scenario where we have to create object of desktops and laptops made by different manufacturer. For this example, we will write code for only two types of computers and their manufacturer.

Computer (Laptop/ Desktop) Type:

  • Laptop: Gaming Laptop, Normal Laptop
  • Desktop: Gaming Desktop, Normal Desktop

Manufacturer:

  • Lenovo
  • Dell

For each type of manufacturer four type of computer object will be created.

  • Gaming laptop from Lenovo or DELL
  • Gaming Desktop from Lenovo or DELL
  • Normal Laptop from Lenovo or DELL
  • Normal Desktop for Lenovo or DELL

Class Diagram

To implement above mentioned problem, I have created two ConcreteFactory classes one for each manufacturer. Same as manufacturer, I have added two product interface; one for laptop and one for desktop.

Abstract Factory design pattern in C# - Class Diagram with water mark

Participants are:

  • IComputerFactory: AbstractFactory interface
  • DellFactory, LenovoFactory: ConcreteFactoryA and ConcreteFactoryB
  • ILaptop, IDesktop: AbstractProductA and AbstractProductB
  • GamingLaptop, NormalLaptop, GamingDesktop, NormalDesktop: ProductA1, ProductA2, ProductB1 and ProductB2
  • ComputerClient: Client

Abstract Factory Design Pattern Implementation – C#

Result

Abstract Factory design pattern in C# - Result

Thanks for reading this article about how to implement abstract factory method design pattern in C#. Please post your comments for queries and feedback. Thanks for reading.

Share Button
The following two tabs change content below.
Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

Gyanendu Shekhar

Gyanendu Shekhar is a technology enthusiast. He loves to learn new technologies. His area of interest includes Microsoft technologies, Augmented reality, Virtual reality, unity3d and android development.

2 thoughts on “Abstract Factory Design Pattern in C#

  • June 13, 2017 at 2:00 pm
    Permalink

    line no : 155 – dellClient = new ComputerClient(lenovoFactory, “Normal”);
    is it right ??

    Reply

Leave a Reply

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