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

建造者模式

时间:2015-12-30 22:00:14      阅读:205      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

class BugerPart
    {
    }

    public class KFCBuger : part
    {
        public override void Make()
        {
            Console.WriteLine("KFC汉堡,递到了客户面前");
        }
    }

    public class MBuger : part
    {
        public override void Make()
        {
            Console.WriteLine("麦当劳汉堡,递到了客户面前");
        }
    }

 

class ChipsPart
    {
    }

    public class KFCChips : part
    {
        public override void Make()
        {
            Console.WriteLine("KFC薯条,递到了客户面前");
        }
    }

    public class MChips : part
    {
        public override void Make()
        {
            Console.WriteLine("麦当劳薯条,递到了客户面前");
        }
    }

 

class Client
    {
        static void Main(string[] args)
        {
            MealKFCBuilder kfcbuilder = new MealKFCBuilder();
            MealDirector kfcdirector = new MealDirector(kfcbuilder);
            kfcdirector.CreateMeal().Make();

            Console.WriteLine("kfc套餐打包完毕,用户得到kfc套餐。");
            Console.Read();
        }
    }

 

class DrinkPart
    {
    }

    public class KFCDrink : part
    {
        public override void Make()
        {
            Console.WriteLine("KFC饮料,递到了客户面前");
        }
    }

    public class MDrink : part
    {
        public override void Make()
        {
            Console.WriteLine("麦当劳饮料,递到了客户面前");
        }
    }

 

public class Meal
    {
        protected List<part> _parts = new List<part>();

        public void AddPart(part part1)
        {
            _parts.Add(part1);
        }

        public void Make()
        {
            foreach (part part1 in _parts)
            {
                part1.Make();
            }
        }
    }

 

public abstract class MealBuilder
    {
        protected Meal _meal = new Meal();

        public abstract void BuildBuger();
        public abstract void BuildChips();
        public abstract void buildDrink();

        public Meal GetMeal()
        {
            return _meal;
        }
    }

 

public class MealDirector
    {
        private MealBuilder _mealbuilder;

        public MealDirector(MealBuilder mealbuilder)
        {
            _mealbuilder = mealbuilder;
        }

        public Meal CreateMeal()
        {
            _mealbuilder.BuildBuger();
            _mealbuilder.BuildChips();
            _mealbuilder.buildDrink();

            return _mealbuilder.GetMeal();
        }
    }

 

public class MealKFCBuilder:MealBuilder
    {
        public override void BuildBuger()
        {
            _meal.AddPart(new KFCBuger());
        }

        public override void BuildChips()
        {
            _meal.AddPart(new KFCChips());
        }

        public override void buildDrink()
        {
            _meal.AddPart(new KFCDrink());
        }

    }

 

public class MealMBuilder:MealBuilder
    {
        public override void BuildBuger()
        {
            _meal.AddPart(new MBuger());
        }

        public override void BuildChips()
        {
            _meal.AddPart(new MChips());
        }

        public override void buildDrink()
        {
            _meal.AddPart(new MDrink());
        }
    }

 

public abstract class part
    {
         public abstract void Make();
    }

建造者模式

标签:

原文地址:http://www.cnblogs.com/victorchi/p/5089977.html

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