Sunday, June 20, 2021

C# Design Pattern - Factory Method Design Pattern



Features of Factory Method Design Pattern
  1. Factory Method Design Pattern is a creational design pattern.
  2. In this design pattern, there is an interface which is derived by classes of different types of objects. The interface contains methods to get generic details of each type of objects to be created. For example, Garment is the super Interface which contains methods to get type of garment and units of garment. Now to create each type of derived classes, abstract factory class or interface is used.
  3. Interface factory or abstract factory class  is used which is derived by concrete factories. The interface factory does not create objects. It is the responsibility of concrete factory classes.
Example: We have generic garment IGarment and generic factory IGarmentFactory. The generic IGarment contains the methods to get type of Garment and units of Garment. The IGarmentFactory contains abstract method which is implemented by each subtype class of IGarment. 

using System;
namespace FactoryMethodDesign
{
    public interface IGarment
    {
        string getGarmentType();
        int getGarmentUnits();
    }
//Concrete class
    public class Shirt : IGarment
    {
        public string getGarmentType()
        {
            return "Shirt";
        }

        public int getGarmentUnits()
        {
            return 2;
        }
    }
//Concrete class
    public class Pant : IGarment
    {
        public string getGarmentType()
        {
            return "Pant";
        }

        public int getGarmentUnits()
        {
            return 3;
        }
    }

    // Factory class is an interface or abstract class in Factory Mathod Design
    // protected abstract method GetGarment will be implemented by derived class
    public abstract class IGarmentFactory
    {
        protected abstract IGarment GetGarment();
        public IGarment CreateGarment()
        {
            return this.GetGarment();
        }
    }
//Concrete Factory/Creator class
    public class ShirtFactory : IGarmentFactory
    {
        protected override IGarment GetGarment()
        {
            IGarment garment = new Shirt();
            return garment;
        }
    }
//Concrete Factory/Creator class
    public class PantFactory : IGarmentFactory
    {
        protected override IGarment GetGarment()
        {
            IGarment garment = new Pant();
            return garment;
        }
    }

//Client class
    class Program
    {
        static void Main(string[] args)
        {
            IGarment garment = new ShirtFactory().CreateGarment();
            Console.Write(garment.getGarmentType());
            Console.Write(garment.getGarmentUnits());
            Console.WriteLine();
            IGarment garment2 = new PantFactory().CreateGarment();
            Console.Write(garment2.getGarmentType());
            Console.Write(garment2.getGarmentUnits());
            Console.ReadKey();
        }
    }
}



No comments:

Post a Comment

Hot Topics