Different ways of applying Factory Method Design Pattern using ASP.NET Core C#
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!