Here we will discuss the Factory Design Pattern in C#. it is a type of Creational Design Pattern.
Factory Design Pattern in C#?
As per Gang of Four, Factory Design Pattern is that “Define an interface for creating an object but let sub-classes decide which class to instantiate. The Factory method class defers instantiation it uses to the sub-classes”. In the Factory pattern, we create product objects without exposing the logic to the end user.
Without creating Factory Design Pattern in C#
public interface Cars { string
GetCarModel(); string
GetColor(); int
GetCarPrice(); } |
2. Now 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; } } |
static void Main(string[] args) {
Console.WriteLine("Enter the Car Model : "); string
carModel = Console.ReadLine(); Cars
carsDetails = null; if
(carModel.ToLower() == "honda") {
carsDetails = new HondaModelCars(); } else if
(carModel.ToLower() == "bmw") {
carsDetails = new BMWModelCars(); } else if
(carModel.ToLower() == "ferrari") {
carsDetails = new FerrariModelCars(); } if
(carsDetails != null) {
Console.WriteLine("Car Model : " +
carsDetails.GetCarModel());
Console.WriteLine("Car color : " +
carsDetails.GetColor());
Console.WriteLine("Car price :" +
carsDetails.GetCarPrice()); } else {
Console.Write("Car Model is not available"); }
Console.ReadLine(); } |
In the above code, we see it is tightly coupled and we can see extra if-else conditions.
Factory Design Pattern Implementation in C#:
public class CarFactory { public static
Cars GetCarModelInfo(string CarModelName) { Cars
carsDetails = null; if
(CarModelName.ToLower() == "honda") {
carsDetails = new HondaModelCars(); } else if
(CarModelName.ToLower() == "bmw") {
carsDetails = new BMWModelCars(); } else if
(CarModelName.ToLower() == "ferrari") {
carsDetails = new FerrariModelCars(); } return
carsDetails; } }
Console.WriteLine("Car Model : " +
carsDetails.GetCarModel());
Console.WriteLine("Car color : " +
carsDetails.GetColor());
Console.WriteLine("Car price :" +
carsDetails.GetCarPrice()); } else {
Console.Write("Car Model is not available"); }
Console.ReadLine(); } |
program.cs class
static void
Main(string[] args) {
Console.WriteLine("Enter the Car Model : "); string
carModel = Console.ReadLine(); Cars
carsDetails = CarFactory.GetCarModelInfo(carModel); if
(carsDetails != null) {
Console.WriteLine("Car model : " +
carsDetails.GetCarModel());
Console.WriteLine("Car color : " +
carsDetails.GetColor());
Console.WriteLine("Car Price :" +
carsDetails.GetCarPrice()); } else {
Console.Write("Invalid Car Model"); }
Console.ReadLine(); } |
EmoticonEmoticon