Different ways of applying Factory Method Design Pattern using ASP.NET Core C#

Sriram Kumar Mannava
2 min readMay 8, 2023

--

What is Factory Method Design Pattern?

Factory Method is one of the 23 Gang of Four (GoF) Design Patterns. It is categorized as a Creational Design Pattern.

Factory pattern helps to design loosely coupled components by separating Instantiation logic from the business logic.

It states to define an interface or abstract class for creating an object but let the sub classes decide which class to instantiate

Different Ways of Implementing a Factory

Approach 1 — Use an else-if ladder

In this approach, different Concrete variants of a component. There is one Factory Method that takes an input. This method switches and returns instance during runtime.

Approach 2 — Use a Dictionary

In this approach, different Concrete variations of the component are seeded as Key Value pairs of an internal Dictionary.

The instance stored for the passed key is returned during runtime. It is faster than using an else-if, since the instances are already created and stored.

Approach 3 — Using Type constrains

In this approach, different variants are all setup to be sub classes of the same type. We simply pass the required Type as type arguments, the Factory method returns a new instance of the Type. There is no need for any conditions!

Full Article — https://referbruv.com/blog/exploring-design-patterns-factory-method/

Share it with your friends, if you find it useful!

--

--

Sriram Kumar Mannava
Sriram Kumar Mannava

Written by Sriram Kumar Mannava

I make Full Stack Development Easy for You | Full Stack .NET Dev | 3× AWS Certified | Blogger

No responses yet