Factory Method Design Pattern in C# with Real-time Example
What is Factory Method Design Pattern?
According to Gang of Four
Definition “Define an interface for
creating an object, but let the subclasses decide which class to instantiate.
The Factory method lets a class defer instantiation it uses to subclasses”.
Understanding the Factory Method Design Pattern with Real-time Example:
we will create classes which will be inherited from the Car interface.
Implementing the Factory Method Design Pattern in C#
public interface Cars { string
GetCarModel(); string
GetColor(); int
GetCarPrice(); } |
2. Now lets create CarModel classes
1. HondaModelCars.cs
public class HondaModelCars : Cars { public string GetCarModel() { return "Honda Model"; } public string GetColor() { return "Red"; } public int GetCarPrice() { return 100000; } } |
2. BMWModelCars.cs
public class BMWModelCars : Cars { public string GetCarModel() { return "BMW Model"; } public string GetColor() { return "Black"; } public int GetCarPrice() { return 200000; } } |
public class FerrariModelCars : Cars { public string GetCarModel() { return "Ferrari Model"; } public string GetColor() { return "Blue"; } public int GetCarPrice() { return 500000; } } |
public abstract class CarFactory { protected abstract
Cars CreateCarProduct(); public
Cars CarDetails() { return this.CreateCarProduct(); } } |
We created the above abstract class with one abstract method i.e. CreateCarProduct() and one concrete method i.e. CarDetails(). The CarDetails() method internally calls the CreateCarProduct() method of the subclass which will create the product instance and return that instance.
public class HondaFactory : CarFactory { protected override
Cars CreateCarProduct() { Cars _model = new
HondaModelCars(); return
_model; } } public class BMWFactory :
CarFactory { protected override
Cars CreateCarProduct() { Cars _model = new
BMWModelCars(); return
_model; } } public class FerrariFactory :
CarFactory { protected override
Cars CreateCarProduct() { Cars _model = new
FerrariModelCars(); return _model; } } |
As you can see the above three classes implement the CreateCarProduct method of the CarFactory class. That’s it. We are done with our implementation. Let’s compare the Gang of Four Definition with our example.
According to Gang of Four, we need to define an interface or abstract class for creating an object. In our example, it is an abstract class i.e. CarFactory class. The second part of the definition saying that let the subclasses decide which class to instantiate. In our example, the subclasses are HondaFactory, BMWFactory, and FerraryFactory. So these subclasses will decide which class to instantiate, for example, Honda, BMW, and Ferrary.
static void
Main(string[] args) { Cars _car = new
HondaFactory().CarDetails(); if
(_car != null) {
Console.WriteLine("Car model : " +
_car.GetCarModel()); Console.WriteLine("Car
color : " + _car.GetColor());
Console.WriteLine("Car Price :" +
_car.GetCarPrice()); } else {
Console.Write("Invalid Car Type"); }
Console.WriteLine("--------------"); _car = new
BMWFactory().CarDetails(); if
(_car != null) {
Console.WriteLine("Car model : " +
_car.GetCarModel());
Console.WriteLine("Car color : " +
_car.GetColor());
Console.WriteLine("Car Price :" +
_car.GetCarPrice()); } else {
Console.Write("Invalid Car Type"); }
Console.ReadLine(); } |
Conclusion:
So, today we have learned what Factory method Design Pattern is and why and where can be used with Real World Example.
1 comments:
Write commentsNice and simple way to understand the Factory method design pattern . Thanks
EmoticonEmoticon