Factory Design Pattern In C#

This pattern comes under the creational patterns which are widely used in programming constructs. In other words,  “This Pattern provides the best way to create the ObjectsIn Factory patterns we create the object of the Class without Exposing the Creation/Instantiation Logic to the User who wants to create the Object and then return the newly Created object using the Common Interface which is inherited  by the Class”.

Example

We take the example of a vehicle. As we know, this vehicle is the superior class of all kinds of the vehicles and they have some common attributes such as  engine, wheels, gears etc.



Implementation

  • First of all, we create the Interface named as “IVehicle” which contains two member -- the first one is NumberOfWheels() which Returns the Number of wheels   specific to the Vehicle Type the second one is VehicleType() which describes the Type of the Vehicle either (Car/Bike/Rickshaw).

  • In our Project the First Class is the Program.cs which calls the object of the VehicleFactory.

  • First of all they Get the Input From the Console whether user wants either car/Bike/Rickshaw, then the information is passed to the VehicleFactory class method name as getVehicle which returns the specific object of the car/Bike/Rickshaw according to the condition match but we see that the all classes inherit the IVehicle interface and according to Factory pattern “a Newly created Object using the Common interface is returned” so object is returned in the form of IVechile interface.

  • IVehicle.cs

    Create an interface



  • Create Concrete Classes Which Implement the IVehicle

    Bike.cs



    Car.cs



    Rickshaw.cs



  • Create The Factory to Generate the Objects According to given information

    VehicleFactory.cs



  • We see that we pass the information about the object to the VehicleFactory which is sometimes known as Parametrized Factory VehicleType which gets the parameter of string type from the main class then after that they check the coming inputs and they create the object and then return to the user.

  • This implementation is most easy to create and also useable. But the problem is that let’s say we want to add Truck class; so in the VehcileFactory we change the code to add some new Condition which is sometimes a bad situation, so that’s why we are able to say that “they violate the Open Close Principle”.

  • For more on OCP visit this link.

  • Use the Factory class to create the Object

    Program.cs

Conclusion

Factory is one of the most useable factory patterns but it is truly depending upon the needs of the Application. If the Application architecture needs it then we use the Factory, otherwise they simply enhance the complexity of the Applications. It is depending upon the needs, like let’s say we have a problem in which we want to cast most of the subclass objects to the Base/Super so we shall prefer to use the Factory. As we kno that the Factory violates the OCP principle of the Object Oriented Paradigm so most developers  suggest using the “Class Registration Implementation” which is the good mechanism.


Similar Articles