2.単純ファクトリモード(Simple Factory)

5177 ワード

using System;



namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            //

            FoodFactory _foodfactory = new FoodFactory();

            Food _food = _foodfactory.CreateFood(2);

            _food.MakeFood();

        }

    }



    /// <summary>

    ///  

    /// </summary>

    public class Food

    {

        /// <summary>

        ///  

        /// </summary>

        public virtual void MakeFood()

        {

        }

    }



    /// <summary>

    ///  

    /// </summary>

    public class FoodFactory

    {

        public Food CreateFood(int type)

        {

            Food _food;

            switch (type)

            {

                case 1:

                    _food = new YaBo(); 

                    break;

                default:

                    _food = new YaJia(); 

                    break;

            }

            return _food;

        }

    }



    /// <summary>

    ///  

    /// </summary>

    public class YaBo : Food

    {

        public override void MakeFood()

        {

            Console.WriteLine(" ");

            Console.ReadLine();

        }

    }



    /// <summary>

    ///  

    /// </summary>

    public class YaJia : Food

    {

        public override void MakeFood()

        {

            Console.WriteLine(" ");

            Console.ReadLine();

        }

    }

}