Design Pattern – Abstract Factory


According to Gang of Four, abstract factory patterns can be assumed as the factory for creating factories.

Learning Objectives

  1. What is the abstract factory design pattern?

  2. How to write code using the abstract factory design pattern?

  3. How to create a factory provider?

  4. How to create a client application(from the Main method) that uses factory provider


Abstract factory pattern is purely an extension factory method; it’s recommended to go through the factory method before understanding abstract factory design.

  1. Basic knowledge of OOPS concepts.

  2. Any programming language knowledge.

The article demonstrates the abstract factory design pattern using the C# programming language. So, to begin with, C#

Getting Started

Let’s consider the same example of any Bank with account types as Savings and Current accounts. Now let’s implement the above example using the abstract factory design pattern.

Firstly, implement ISavingAccount and ICurrentAccount interfaces as follows:

public interface ISavingAccount{  }
public interface ICurrentAccount{  }

Inherit the interface in classes below

Let’s write an abstract class with abstract methods for each account type.

Now, let’s create a factory implementation named “Bank1Factory,” which provides the implementation of abstract methods.

The abstract factory design pattern differs from the factory method that it needs to implement a factory provider, which returns factories as per definition.

Now that we have all the abstractions and factories created. Let us design the factory provider. Please find below the code snippet for factory provider, where a static method will create a factory based upon account name.

How to use abstract factory provider?

Let’s take an example of a list of account numbers where if an account name consists of “B1” literal, then it will use Bank1Factory instance returned via factory provider.

If the account name does not contain “B1” literal, then the program will output invalid {{accountName}}


Please find below the output from the above code snippet.

Hello saving B1-456
Hello Current B1-456
Hello saving B1-987
Hello Current B1-987

Github Repo

Thank you for reading, and I hope you liked the article. Please provide your feedback in the comment section.

Follow me on C# Publication, LinkedIn, Instagram, Twitter,, Pinterest, Substack, Wix.

#AbstractFactoryPattern #Coding #DesignPatterns #Dotnet #Programming

Recent Posts

See All

Design Pattern – Adapter

#Aspnetcore #AdapterDesignPattern #Csharp #DesignPatterns #Dotnet According to Gang of Four, the Adapter Pattern converts the interfaces of a class into interfaces that the client requires. In other w