Friday, May 6, 2016

Simple Example of Abstract Factory Design Pattern in C#




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Interface_Implement;

namespace AbstractFactoryPattern
{

    class Program
    {

        static void Main(string[] args)
        {
            //Client Code
            Console.WriteLine("***************HighBudgetMachine****************");
            IMachineFactory factory = new HighBudgetMachine();// Or new LowBudgetMachine();
            ComputerShop shop = new ComputerShop(factory);
            shop.AssembleyMachine();

            Console.WriteLine("\n***************LowBudgetMachine****************");
            factory = new LowBudgetMachine();// Or new HighBudgetMachine();
            shop = new ComputerShop(factory);
            shop.AssembleyMachine();

            Console.WriteLine("\n***************AverageBudgetMachine****************");
            factory = new AverageBudgetMachine();// Or new HighBudgetMachine();
            shop = new ComputerShop(factory);
            shop.AssembleyMachine();


       
            Console.ReadKey();
        }    
     
    }

    #region "Interface and implimantation"
    public interface IProcessor
    {
        void PerformOperation();
    }
    public interface IHardDisk { void StoreData();}
    public interface IMonitor { void DisplayPicture();}
 

    public class ExpensiveProcessor : IProcessor
    {
        public void PerformOperation()
        {
            Console.WriteLine("Operation will perform Quickly");
        }
    }
    public class CheapProcessor : IProcessor
    {
        public void PerformOperation()
        {
            Console.WriteLine("Operation will perform Slowly");
        }
    }
    public class ExpensiveHDD : IHardDisk
    {
        public void StoreData()
        {
            Console.WriteLine("Data Will take less time to store");
        }
    }
    public class CheapHDD : IHardDisk
    {
        public void StoreData()
        {
            Console.WriteLine("Data will take more time to store");
        }
    }
    public class HighResolutionMonitor : IMonitor
    {
        public void DisplayPicture()
        {
            Console.WriteLine("Picture quality is Best");
        }
    }
    public class LowResolutionMonitor : IMonitor
    {
        public void DisplayPicture()
        {
            Console.WriteLine("Picture quality is Average");
        }
    }
    #endregion

    #region "Factory Code will be as follows"
    public interface IMachineFactory
    {
        IProcessor GetRam();
        IHardDisk GetHardDisk();
        IMonitor GetMonitor();
    }
    public class HighBudgetMachine : IMachineFactory
    {
        public IProcessor GetRam() { return new ExpensiveProcessor(); }

        public IHardDisk GetHardDisk() { return new ExpensiveHDD(); }

        public IMonitor GetMonitor() { return new HighResolutionMonitor(); }
    }

    public class LowBudgetMachine : IMachineFactory
    {
        public virtual IProcessor GetRam() { return new CheapProcessor(); }
        public IHardDisk GetHardDisk() { return new CheapHDD(); }
        public IMonitor GetMonitor() { return new LowResolutionMonitor(); }
    }
    //Let's say in future...Ram in the LowBudgetMachine is decided to upgrade then
    //first make GetRam in LowBudgetMachine Virtual and create new class as follows

    public class AverageBudgetMachine : LowBudgetMachine
    {
        public override IProcessor GetRam()
        {
            return new ExpensiveProcessor();
        }
    }
    #endregion


    #region
    public class ComputerShop
    {
        IMachineFactory category;
        public ComputerShop(IMachineFactory _category)
        {
            category = _category;
        }

        public void AssembleyMachine()
        {
            IProcessor processor = category.GetRam();
            IHardDisk hdd = category.GetHardDisk();
            IMonitor monitor = category.GetMonitor();

            //use all three and create machine
            processor.PerformOperation();
            hdd.StoreData();
            monitor.DisplayPicture();
        }
    }
    #endregion

}

1 comment:

Saurabh Tripathi said...

Thanks for sharing your fabulous idea. This blog is really very useful.Asp.net Application Company