Abstract Factory Design Pattern – C#

Abstract Factory method pattern falls under Creational Pattern of Gang of Four (GOF) Design Patterns in .Net. It is used to create a set of related objects, or dependent objects. Internally, Abstract Factory use Factory design pattern for creating objects. It may also use Builder design pattern and prototype design pattern for creating objects. It completely depends upon your implementation for creating objects. In this article, I would like share what is abstract factory pattern and how is it work?

What is Abstract Factory Pattern?

Abstract Factory patterns acts a super-factory which creates other factories. This pattern is also called as Factory of factories. In Abstract Factory pattern an interface is responsible for creating a set of related objects, or dependent objects without specifying their concrete classes.

Abstract Factory Pattern – UML Diagram & Implementation

The UML class diagram for the implementation of the abstract factory design pattern is given below:

The classes, interfaces and objects in the above UML class diagram are as follows:

  1. AbstractFactory

    This is an interface which is used to create abstract product

  2. ConcreteFactory

    This is a class which implements the AbstractFactory interface to create concrete products.

  3. AbstractProduct

    This is an interface which declares a type of product.

  4. ConcreteProduct

    This is a class which implements the AbstractProduct interface to create product.

  5. Client

    This is a class which use AbstractFactory and AbstractProduct interfaces to create a family of related objects.

C# – Implementation Code

Abstract Factory Pattern – Example

Who is what?

The classes, interfaces and objects in the above class diagram can be identified as follows:

  1. VehicleFactory – AbstractFactory interface
  2. HondaFactory & HeroFactory– Concrete Factories
  3. Bike & Scooter – AbstractProduct interface
  4. Regular Bike, Sports Bike, Regular Scooter & Scooty – Concreate Products
  5. VehicleClient – Client

C# – Sample Code

Abstract Factory Pattern Demo – Output

When to use it?

  1. Create a set of related objects, or dependent objects which must be used together.
  2. System should be configured to work with multiple families of products.
  3. The creation of objects should be independent from the utilizing system.
  4. Concrete classes should be decoupled from clients.

Note

  1. Internally, Abstract Factory use Factory design pattern for creating objects. But it can also use Builder design pattern and prototype design pattern for creating objects. It completely depends upon your implementation for creating objects.
  2. Abstract Factory can be used as an alternative to Facade to hide platform-specific classes.
  3. When Abstract Factory, Builder, and Prototype define a factory for creating the objects, we should consider the following points :
    • Abstract Factory use the factory for creating objects of several classes.
    • Builder use the factory for creating a complex object by using simple objects and a step by step approach.
    • Prototype use the factory for building a object by copying an existing object.

Source:dotnettricks.com

Please follow and like us:

Leave a reply

Your email address will not be published.