The Factory Design Pattern is one of the most frequently used design patterns in real-time applications. The Factory Design Pattern in C# falls under the Creational Design Patterns Category.
What is Factory Design Pattern in C#?
Let us first try to understand the definitions of the factory design pattern.
According to Gang of Four (GoF), the Factory Design Pattern states that A factory is an object used for creating other objects. In technical terms, we can say that a factory is a class with a method. That method will create and return different objects based on the received input parameter.
In simple words, when we have a main class(super class) and several different types of classes(subclasses) that are related to it, and we want to make an object from one of these related classes based on some information, we use something called the Factory Design Pattern in C#.
The Key Components:
- Product Interface/Abstract Class:This defines an interface or an abstract class for the products generated by the factories. Product classes usually share common attributes or methods.
- Concrete Products:These are the real-deal classes that implement the product interface. Each concrete product embodies a specific type of object.
- Factory Class:This class contains a method for creating objects.Typically, this method is named something like 'createProduct()' or 'factoryMethod()'.