Example #1
0
        static void Main(string[] args)
        {
            //下订单:2巧克力 1牛奶 的 longblack

            //1 longblack
            Drink order = new LongBlack();

            Console.WriteLine("费用:\t" + order.Cost());
            Console.WriteLine("描述:\t" + order.GetDescription());

            //2 加牛奶
            order = new Milk(order);
            Console.WriteLine("费用:\t" + order.Cost());
            Console.WriteLine("描述:\t" + (order as Decorator).GetDescription());

            //3 加巧克力
            order = new Chocolate(order);
            Console.WriteLine("费用:\t" + order.Cost());
            Console.WriteLine("描述:\t" + (order as Decorator).GetDescription());

            //3 加巧克力
            order = new Chocolate(order);
            Console.WriteLine("费用:\t" + order.Cost());
            Console.WriteLine("描述:\t" + (order as Decorator).GetDescription());

            Console.ReadLine();
        }
Example #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            //单品黑咖啡
            Coffee coffee = new LongBlack();

            MyPrint(coffee);

            //黑咖啡的基础上+牛奶
            coffee = new Milk(coffee);//生成装饰对象(牛奶)的同时将之前被装饰者(黑咖啡)注进去,达到累加效果
            MyPrint(coffee);

            //依此类推
            //可以继续添加装饰者
            coffee = new Chocolate(coffee);
            MyPrint(coffee);

            Console.ReadLine();
        }