Factory Method Design Pattern in C#


in this article, we will discuss Factory Method Design Pattern in C# with examples. It is a Creational Design Pattern. Previously we discuss the Factory Design Pattern which is also a part of the Creation design pattern.

What is Factory Method Design Pattern?

As per Factory Method Design Pattern is used when we want to create the object of the class without exposing the logic of object creation to the client. 

According to the Gang of Four Definition “Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method class defer instantiation it uses to subclasses”.

to implement it in the factory method design pattern we need to create the abstract class as part of the Factory class which will create an object and return the object of the product class, and here subclasses will decide which class needs to instantiate. 

Factory Method Design Pattern with Example:
To understand the factory design pattern, will create a couple of the classes and an interface, called CarInterface, this interface will have the below methods

        string GetCarModel();
        string GetColor();
        int GetCarPrice(); 


In Factory Method Design Pattern, we have to create an abstract class or interface for creating the object.







As above CarFactory abstract class has two methods, one abstract method i.e. CarInfo(), and one concrete method i.e. CarDetails(). The CarDetails() method internally calls the CarInfo() method of the subclass which will create the product instance and return that instance.

here we have three car models (i.e. Honda, BMW, and Ferrari), so here we created three subclasses (i.e. HondaFactory, BMWFactory, and FerrariFactory) of the Abstract CarFactory class and implement the CreateCarProduct() method. This method is going to return the actual product object i.e. (Honda, BMW, and Ferrari).



Below is the Client code, from where we will call the concrete class



Implementing the Factory Method Design Pattern in C#
Create a Factory class


public interface Cars

{

    string GetCarModel();

    string GetColor();

    int GetCarPrice();

}

 2. Now let's 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;

        }

    }
3. FerrariModelCars.cs

 public class FerrariModelCars: Cars


    {

        public string GetCarModel()

        {

            return "Ferrari Model";

        }

        public string GetColor()

        {

            return "Blue";

        }

        public int GetCarPrice()

        {

            return 500000;

        }

    }



Create Abstract class


public abstract class CarFactory

{

    protected abstract Cars CreateCarProduct();

    public Cars CarDetails()

    {

        return this.CreateCarProduct();

    }

}


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 classes implement the CreateCarProduct method of the CarFactory class and call from the program.cs class


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();

        }

After running this, below is the output.



Share this

Related Posts

Previous
Next Post »

1 comments:

Write comments
Anonymous
February 6, 2022 at 12:59 AM delete


Nice and simple way to understand the Factory method design pattern . Thanks

Reply
avatar