Abstract Factory Pattern In C#

Abstract Factory Pattern

This pattern comes under creational patterns which are widely used in programming constructs: “This Pattern provide the best way to create the Objects.

Abstract Factory Patternis used to make the Abstract Factory Class (Base-Factory) which is inherited by the Sub Factories class. All objects of the Sub Factories are accessible with the help of the Abstract Factory. The Abstract Factory is sometimes called Factory of Factories (Sub Factories). Only Interface is responsible to create the group of related object without specifying their class. Sub Factories generate the specific Object with the help of interface”.

Explanation (for complete understanding)

  • In my previous article, I took the example of the Vehicle factory which produces the sub objects of car/Bike/Rickshaw which is inherited by the IVehicle Interface. We request the VehicleFactory which generates the respective objects and we must specify what kind of object we need, either car/Bike/Rickshaw.

  • Let’s say we talk about air vehicles and sea vehicles. At this point the Factory pattern is not able to fulfill the requirements nor tell the complete information about the sea or air vehicle because of the specifies to the specific type of the vehicle either sea, air, or road, because modularization is one of the key principles which applies when we design the software.

  • So what do we the programmers think we should make the super Factories which are responsible to create the sub factories so the sub factories further give the information about the objects using the “Interface" if we want to get information about the engine? 

  • At that point first of all the use is Interacting with the Abstract Factory.  The abstract factory tells the user what kind of vehicle type information you want. If we say, ok give me the information about the engine of the Passenger ship, the Abstract Factory returns the object of the sea Vehicle Factory which is the sub-Class (Sub-Factory) of the Abstract Factory class which contains the all information of the engine regarding the sea Vehicles.

  • So we get the object of the sub Factory, which is responsible for generating the object of the specific type in the form of IVehicle interface Object.


We have 3 types of vehicle families,



  • UML Diagram

  • First of all we create the Interface named IVehciles.cs which contains only 1 Method named GetEngineInfo():String. This method is responsible for giving information about the engine of the specific type of the Vehicle. “This interface is responsible to create the Families of Related Objects without Specifying their Classes

  • Then after that we make the AbstractFactoryClass which is abstract class Named AbstractFactory which contains 3 abstract methods of return type IVehicles Interface. The first method returns the Factory of SeaVehicles and the 2nd one is AirVehicles and 3rd one is RoadVehicles. This abstract class is inherited by Sub Factories.

  • Then we make the AbstractFactoryProduction.cs class which is responsible to give the object the objects of the subFactories which depends upon the type of the subFactory. Let’s say we want to get the object of the SeaVehcileFactory, so we call the static method of the AbstractFactoryClass simply just pass the type and they will return the SubFactory Object in the form of parent Factory Object. The Parent Abstract encapsulates the object of sub factories and then this object is returned.

    The Abstract Factory class is used to check the behavior of the user because they Abstract Factory class determines the actual type of the Concrete class which is inherited by some interface which is responsible to create the groups of objects related to the particular family

    The return type of that method clearly shows that the sub factories return in the form of the parent factory so at run time the user does not have any idea about what kind of object abstract Factory contains so this is the another way to secure the child object, only they will return in the form of the parent object. This means that there is no longer need to declare the class type and call their constructor using the new keyword because we only work with the Parent Object.

  • SeaVehicleFactory which is the sub class of the AbstractFactory returns the object of the concrete classes in the form of the IVehicles interface.

  • AirVehicleFactory is the sub class of the AbstractFactory returns the object of the concrete classes in the form of the IVehicles interface.

  • RoadVehicleFactory is the sub class of the AbstractFactory returns the object of the concrete classes in the form of the IVehicles interface.

  • Bus.cs

  • Car.cs

  • Ship.cs

  • OilTanker.cs

  • AirPlane.cs

  • HeliCopter.cs

  • Program.cs main class is responsible for the execution of the entire system.

Pros and Cons of the Abstract Factory

  1. Abstract Factory provides the best way of encapsulation in the form of abstract pointer.

  2. Loosely Coupled Application: It means in future we just change the implementation of the base abstract and make the new family by inheriting the base.

  3. Also provides Polymorphism, since we know that inheritance and poly go hand in hand.

  4. The family of the similar objects works together.

  5. When we use the abstract factory all things become much difficult and code is hard to read so this is the issue with the AFP (Abstract Factory Pattern), they enhance the complexity as well.

  6. Due to the loose coupling we make the extra objects which decrease the performance of the application.


Abstract Factory is one of the most usable Design Pattern when we need the higher degree of Abstraction but it is truly dependent upon the needs of the application. If the application architecture needs it, if Application is closed to work with the families of the similar object then we use the Abstract Factory otherwise if we use so they simply enhance the complexity of the applications.

Up Next
    Ebook Download
    View all
    View all