Factory Method Design Pattern with an Example in C#.Net


   Like Singleton Design Pattern, Factory Method Design Pattern also one of the well known pattern. Please have a look on below example.

Here we will write an interface for the creation of the objects. But, in this scenario subclasses will class to be instantiate and through common interface we will refer to the new created object.

Neeful approaches:
1. Whenever we require concrete classes that must be instantiated by factory subclasses.
2. whenever we require factory class should not know what will require for creation of concrete class. Delegates to its subclasses  for the creation of the concreate class.
3. Whenever we require a framework delegate for recreation of the objects from the superclass to the factory class. 


Man and Women class is implementing iHuman interface. HumanFactory is the factory class for sending instantiation of the class.
 interface iHuman
    {
        void Quality1();
        void Quality2();
    }

    class Man:iHuman
    {
        public Man()
        {
            Console.WriteLine("Man constructor");
        }
        #region iFactory Members
        public void Quality1()
        {
            Console.WriteLine("Man can ride bike");
        }

        public void Quality2()
        {
            Console.WriteLine("Man can run quickly");
        }
        #endregion
    }

    class Women : iHuman
    {
        public Women()
        {
            Console.WriteLine("Women Constructor");
        }
        #region iFactory Members

        public void Quality1()
        {
            Console.WriteLine("Women sings a song well");
        }

        public void Quality2()
        {
            Console.WriteLine("Women do dance well");
        }

        #endregion
    }


    class Program
    {
        public enum RealHuman { man, women }
        static void Main(string[] args)
        {
            iHuman iMan;
            iHuman iWoman;
            iMan=HumanFactory.HumanType(FactoryDesignPattern.HumanFactory.Human.Man);
            iWoman = HumanFactory.HumanType(FactoryDesignPattern.HumanFactory.Human.women);
            iMan.Quality1();
            iWoman.Quality2();

            Console.ReadLine();
        }
    }

    class HumanFactory
    {
        public enum Human
        {
            Man,women
        }
        static HumanFactory()
        {}
        public static iHuman HumanType(Human TypeOfHuman)
        {
            switch (TypeOfHuman)
            {
                case Human.Man: return new Man();
                case Human.women: return new Women();
                default: throw new ArgumentException("invalid selection");
            }
        }
    }

Factory Method Design Pattern with an Example in C#.Net
Factory Method Design Pattern with an Example in C#.Net


If you have any queries or suggestions, please fell free to ask in comments section.
Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar