Sunday, September 1, 2024

Factory Pattern



Factory pattern is one of the most used creational design pattern in real world applications, like DB, Logging frameworks. It is used to create objects based on a common interface.

Without Factory

End user is responsible for creating the object by directly instantiating the class based on the input type during its construction.  It has limitation of tightly coupling, limited scalability and single responsibility violation.

<pre><code>

if (carModel == 1)

{

_evCar = new Tesla();

}

</code></pre>

With Factory

Two key actors in Factory design (1) underlying product (2) creator factory. Both actors are driven by interface with multiple derived classes.

It is a design pattern that provides an interface for creating objects in a superclass,  but allows subclasses to alter the type of objects that will be created.

<pre><code>

ICarFactory carFactory = new CarFactory();

_evCar = carFactory.GetCar(carModel);

</code></pre>

Benefits

The Factory pattern is useful when you need to create an object based on a specific input or condition. It has few design advantages like: decoupling of creation logic with client usage, code reusability, scalability with extensibility and single responsibility principle.

Reference

Code repository: https://github.com/gsenthilvel/DesignCreational/tree/main/factory 


No comments:

Post a Comment