码迷,mamicode.com
首页 > 其他好文 > 详细

抽象工程模式

时间:2016-01-03 21:00:30      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

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

namespace KFCandM
{
    class Program
    {
        interface IFactory
        {
            Burger Makeburger();
            Drink Makedrink();
            Chicken Makechicken();
        }

        class KFC : IFactory
        {
            public Burger Makeburger()
            {
                return new Kburger();
            }

            public Drink Makedrink()
            {
                return new Kdrink();
            }

            public Chicken Makechicken()
            {
                return new Kchicken();
            }
        }

        class MacDonald : IFactory
        {
            public Burger Makeburger()
            {
                return new Mburger();
            }

            public Drink Makedrink()
            {
                return new Mdrink();
            }

            public Chicken Makechicken()
            {
                return new Mchicken();
            }
        }


        abstract class Burger
        {
            public abstract void burger();
        }

        class Kburger : Burger
        {
            public override void burger()
            {
                Console.WriteLine("KFC‘s Hamburger is ready...");
            }
        }


        class Mburger : Burger
        {
            public override void burger()
            {
                Console.WriteLine("MacDonald‘s New Orleans Beef Burger is ready...");
            }
        }

        abstract class Drink
        {
            public abstract void drink();
        }
        class Kdrink : Drink
        {
            public override void drink()
            {
                Console.WriteLine("KFC‘s coffee is ready...");
            }
        }

        class Mdrink : Drink
        {
            public override void drink()
            {
                Console.WriteLine("MacDonald‘s orange juice is ready...");
            }
        }
        abstract class Chicken
        {
            public abstract void chicken();
        }

        class Kchicken : Chicken
        {
            public override void chicken()
            {
                Console.WriteLine("KFC‘s  New Orleans chicken wings are ready...");
            }
        }

        class Mchicken : Chicken
        {
            public override void chicken()
            {
                Console.WriteLine("MacDonald‘s spicy chicken wings are ready...");
            }
        }

        class Food
        {
            private Burger _burger;
            private Drink _drink;
            private Chicken _chicken;

            public Burger Burger
            {
                get { return _burger; }
                set { _burger = value; }
            }

            public Drink Drink
            {
                get { return _drink; }
                set { _drink = value; }
            }

            public Chicken Chicken
            {
                get { return _chicken; }
                set { _chicken = value; }
            }
            public void Eatting()
            {
                _chicken.chicken();
                _burger.burger();
                _drink.drink();
                Console.WriteLine("The lunch is over");
            }

            private static void Main(string[] args)
            {
                IFactory factory1 = new KFC();
                IFactory factory2 = new MacDonald();

                Food f1 = new Food();
                f1.Burger = factory1.Makeburger();
                f1.Chicken = factory1.Makechicken();
                f1.Drink = factory1.Makedrink();
                f1.Eatting();

                Food f2 = new Food();
                f2.Burger = factory2.Makeburger();
                f2.Chicken = factory2.Makechicken();
                f2.Drink = factory2.Makedrink();
                f2.Eatting();

                Console.ReadLine();

            }

        }
    }
}

运行的结果

技术分享

抽象工程模式

标签:

原文地址:http://www.cnblogs.com/zhouxixi/p/5097106.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!