Factory Design Pattern Implementation in C#

Asad Iftikhar
3 min readJun 12, 2021

What is Factory Design Pattern?

In Factory pattern, we create object without exposing the creation logic to client and the client use the same common interface to create new type of object.

Factory method is a creational design pattern which solves the problem of creating product objects without specifying their concrete classes.

When to Use Factory Pattern

  1. when you have Several classess and you want to obtain one of those classess which share a common Super Class.
  2. The main reason of Factory method is that the class will be chosen at run time
  3. When you don't know which object to be created.
  4. Centralized class object don't want the user to know every subclasses.

Now we will do some Practical Explanation of Factory Design Pattern

Suppose you have Video game which they have multiple Enemies ships for Destroying the Target ship to achieve its goal

Create a Console Application for .NET Core named it FactoryPatternEnemyShip

Create two Classess in it which which will extend the Super Class
Named the Super Class EnemyShip.cs
1. RocketEnemyShip.cs
2. UFOEnemyShip.cs

Now Open Enemy Ship Super class and Write this Code in it

public abstract class EnemyShip{private string name;private double amtDamage;public string getName() => name;public void SetName(String newName) { name = newName; }public double getDamage() => amtDamage;public void SetDamage(double newDamage) { amtDamage = newDamage; }public void followHeroShip(){Console.WriteLine(getName() + "is following the Hero");}public void DisplayEnemyShip(){Console.WriteLine(getName() + "is on the Screen");}}

Now Open RocketEnemyShip.cs and type this code

public class RocketEnemyShip : EnemyShip{public RocketEnemyShip(){SetName("Rocket Enemy Ship");SetDamage(40.0);}}

Open BigUFOEnemyShip and write this code

public class BigUFOEnemyShip: EnemyShip{public BigUFOEnemyShip(){SetName("Big UFO EnemyShip");SetDamage(67.0);}}

Create two more Class for Creating Factory Object and calling Factory Methods

  1. EnemyShipTesting.cs
  2. EnemyShipFactory.cs

Open EnemyShipTesting and Write this code in it

public class EnemyShipTesting{public static void CreateFactoryObject(){EnemyShipFactory shipFactory = new EnemyShipFactory();EnemyShip theEnemy = null;Console.WriteLine("Enter type of Ship");string TypeShip = Console.ReadLine();theEnemy = shipFactory.makeEnemyShip(TypeShip);if (String.IsNullOrEmpty(TypeShip) == false)doStuffEnemy(theEnemy);elseConsole.WriteLine("Kindly Select U,R,B");}public static void doStuffEnemy(EnemyShip enemyShip){enemyShip.DisplayEnemyShip();enemyShip.followHeroShip();Console.ReadKey();}}

Open EnemyShip Factory and write this Code in it

public class EnemyShipFactory{public EnemyShip makeEnemyShip(string newShipType){EnemyShip newShip = null;if (newShipType.Equals("U"))return new BigUFOEnemyShip();else if (newShipType.Equals("R"))return new RocketEnemyShip();else if (newShipType.Equals("B"))return new BigUFOEnemyShip();else return null;}}

Open Program.cs write this in your main method

EnemyShipTesting.CreateFactoryObject();
This is the Output After Every thing

Now run the Code and you will achieve this windows Enter B for Big UFO and R for Rocket

you can also achieve this by if and else condition in your main method but then you are giving Client the access to the Sub Classes when they don't need it using Factory Methods

Another Practical Example for this is Database Connectivity

Factory Design pattern is used when you have multiple things of same domain.

like Cards product
For example : - One Debit card has multiple products Normal, Etihad, GenY

surely there data for card embossing application will be different we can use factory pattern to create object
at run time based on their cards criteria.

For example : - In Insurance there are multiple types of vouchers and vouchers objects can be created
at run time by understanding which type of object is Voucher.

--

--