Design Pattern – Factory Method


According to the Gang of Four, the factory method allows the subclass to determine which class object should be created.

Photo by Life Of Pix from Pexels

Learning Objectives

  1. What is the factory method design pattern?

  2. How to write code using the factory method?


  1. Basic knowledge of OOPS concepts.

  2. Any programming language knowledge.

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

View at

Getting Started

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

Firstly, create an account type abstract class.

public abstract class AccoutType
   public string Balance { get; set; }

Implement current and saving account classes inheriting the AccountType abstract class as shown below.

Finally, let’s implement the factory interface, which will provide a contract that helps create a class object. This interface is also known as the Creator.

public interface IAccountFactory
  AccoutType GetAccoutType(string accountName);

At last, write an implementation of the creator interface method as shown below. The class that implements the creator is known as Concrete Creator.

That’s it. You have successfully implemented the factory method using the Bank example.

How to use the factory method?

A subclass will decide which “AccountType ” class object will be created based upon the account name.

For example, if the account name is “SAVINGS,” then the “SavingAccount” class object will be created and returned.

Similarly, if the account name is “CURRENT,” then the “CurrentAccount” class object will be instantiated and returned.


Saving account balance: 10000 Rs
Current account balance: 20000 Rs

Github Repo

View at

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.

#FactoryMethodPattern #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