Abstract Factory Design Pattern with C#



Abstract Factory Design Pattern with C#

Abstract Factory Design Pattern is a creational pattern, as it is used to control class instantiation. ( more details in design patterns categories )

It separates the details of implementation of a set of objects from their usage and relies on composition.

It achieves this by providing a common interface to create related objects without knowing their implementation.

The abstract factory pattern provides a way to encapsulate a group of individual factories that have a common theme without specifying their concrete classes.

The client doesn’t know (or care) which concrete objects it gets from each of these internal factories, since it uses only the generic interfaces of their products.

Example using Abstract Factory Design Pattern

If you use Factory pattern. you will build objects using that factory. the problem arise when you have multiple objects to build from multiple factories. Now comes the abstract factory design pattern.

In a game where we need to instantiate two soldiers, providing each one has his own way of attacking. We could have two different factory patterns to create those two objects.

But with the usage of abstract factory pattern, both of them will inherit from the same interface that has Attack() method and implement it in their own way.

When the client asks for a soldier of kind “Archer” for example and a soldier of kind “Knight”. Then the abstract factory asks the Archer factory to create the archer object and asks the knight factory to create the knight object then returns both created objects to the client.

The client now knows how to trigger the attack method of each one of them as they implement the same interface. and it didn’t care how they are implemented or created.

What Abstract Factory Design Pattern solves ?

  • How to separate the application from the objects that it needs to create.
  • Provide a way to achieve encapsulation of objects creation rather than exposing their implementation details.
  • How families of factory classes cooperate with each other in order to create a set of objects.

Difference between Factory and Abstract Factory Design Patterns

  • Factory Design Pattern
    • Relies on inheritance.
    • Factory method is just a simple method that is used to create objects in a class.
    • creates a single object.
    • The client is a subclass whose parent defines the factoryMethod()This is why it relies on inheritance.
  • Abstract Factory Design Pattern
    • An object that has multiple object on it.
    • The responsibility of object creation is transitioned to another object via composition.
    • creates a family of objects.

You will get a better understanding when we study the Factory design pattern in detail.

Abstract Factory Design Pattern Participants

  • Client. This class uses the factories to generate a family of related objects.
  • AbstractFactory. This is an abstract base class for the concrete factory classes that will generate new sets of related objects.
  • Factory1. Inheriting from the AbstractFactory class, the concrete factory classes override the methods that generate the suite of objects required by the client.
  • Product A. This abstract class is the base class for the types of object which a factory can create for the first product.
  • Product B. This abstract class is the base class for the types of object that a factory can create for the second product.
  • ProductA1/ProductB1. Multiple subclasses of the Product A and Product B classes are defined, With each one containing specific functionality. Objects of these classes are generated by the abstract factory to populate the client.


applying the sequence on the soldier example :

  • Client ask the factory to create two instances of Archer soldier and a Knight soldier.
  • Factory tells the Soldier Factory to create a soldier object.
  • Soldier factory returns back the soldier object to the Abastract factory then to the Client.
  • Same process occurs with Knight soldier.


Abstract Factory Design Pattern Implementation

We are going to implement the pattern in C# language. For that, you need visual studio with .NET framework installed.

I will assume that you know how to open visual studio and install .Net framework.  Otherwise you can follow the official tutorials. Then create a new project from File >> New >> Project

Name it : “Abstract_Factory” or whatever name you want. Then Select the location and click create.

Implementing Soldier Classes

Define a new Interface for soldiers to implement.

Make the Archer Soldier implement the Attack() method

Define the Knight Soldier and implement the Attack() method

Now each soldier knows how to attack and attacks in his own way.

Implementing Soldier Factory Classes

Define IFactory Interface to provide a general method for object creation.

Make Archer Factory implement the Create(() method.

Define the Knight Factory to implement the Create() method.

Now we have two separates factories each one creates a different object.

Implementing the Abstract Factory class

The abstract factory class should contain two methods for creating a knight and an archer soldiers.

It also needs to cache references to the concrete factories to ask them for object creation.

Note the CreateArcher() method and CreateKnight() method both returns an object of type ISoldier, In this way we can have a unified interface to work with and also we don’t have explicitly cast the created objects.

Implementing the client

The client class will need an archer and a knight soldiers. So, it will cache them as variables.

the create method will use the parsed Abstract Factory class to create the objects.

the objects we create will be saved into the variables to be used when needed.

Note archer and knight variables are of type ISoldier to be able to call a unified interface.

Implementing the Main Program

No that we have implemented the pattern we will use, we need to put every thing in action.

Create a new abstract factory object.

Create a new client object and give it the abstract factory we just created so that they can work together building the needed objects.

Now the Client has what it needs of archer and knight objects, It’s time to trigger their attack methods.

Run the application by pressing F5 you will get the following output


Disadvantages of Abstract Factory Design Pattern

Like any design pattern, there is no fit-them-all rule also no silver bullet to do the things in one way.

Abstract Factory Design pattern has some Cons you need to put into consideration when using it :

  • Too much abstraction : since objects are created on run time and factories are chosen on run time ( maybe from an XML file ), This adds up layers of abstraction to our program.
  • It can easily turn into an Anti Pattern : using it the wrong way can turn it into an anti pattern. for example : wiring up the whole application with abstract factory pattern.
  • If there is a change in the specifications of the concrete factories, the Factory interface needs to be modified also. so be careful when designing the general interface.


1- Abstract Factory Pattern