Factory Pattern in C#

What is Factory pattern?

Factory pattern is the most widely used pattern in the software engineering world. This pattern introduces loose coupling between classes which is the most important principle one should consider and apply while designing the application architecture. Loose coupling can be introduced in application architecture by programming against abstract entities rather than concrete implementations. This not only makes our architecture more flexible but also less fragile.

In fact, factory pattern is very common in C# programming. If you check .NET framework or any other frameworks Factory Pattern is widely used which shows its popularity. Factory Pattern belongs to Creational Patterns, and it deals with object creation.¬†When we write codes, there are situations like creating objects based on some if conditions or switch case. If this object creation is not based on a proven design pattern then we are really making the object creation complex and future enhancements very tough. At this time we should consider Factory Pattern to abstract the complexity of the object creation logic, and enabling future additions hassle free.¬†Factory Pattern is nothing but hiding object creation logic from the client so that the end client doesn’t have to worry about object creational logic, rather the client can refer the factory pattern created object using a common interface.

 

Copyright © All Rights Reserved - C# Learners