コード例 #1
0
ファイル: StructType.cs プロジェクト: wmchuang/DesignPattern
        public void DecoratorTest()
        {
            {
                var m          = new ConcreteComponent();
                var decoratorA = new DecoratorA(m);
                // decoratorA.Work();

                var decoratorB = new DecoratorB(decoratorA);
                decoratorB.Work();
            }
            {
                // 我买了个苹果手机
                Phone phone = new ApplePhone();

                // 现在想贴膜了
                var applePhoneWithSticker = new Sticker(phone);
                // 扩展贴膜行为
                applePhoneWithSticker.Print();

                // 现在我想有挂件了
                var applePhoneWithAccessories = new Accessories(phone);
                // 扩展手机挂件行为
                applePhoneWithAccessories.Print();
                Console.WriteLine("----------------------");

                // 现在我同时有贴膜和手机挂件了
                var sticker = new Sticker(phone);
                var applePhoneWithAccessoriesAndSticker = new Accessories(sticker);
                applePhoneWithAccessoriesAndSticker.Print();
            }

            // {
            //     AbstractMessage message = new SqlMessage();
            //     var de = new CheckUserWrapper(message);
            //     Console.WriteLine(de.Get()[0].Message);
            //
            //     var d2 = new CheckInputWrapper(message);
            //     Console.WriteLine(d2.Get()[0].Message);
            //
            //     var m3 = new CheckUserWrapper(message);
            //     var m4 = new CheckInputWrapper(m3);
            //     Console.WriteLine(m4.Get()[0].Message);
            // }

            {
                var dark  = new DarkRoast("黑咖啡");
                var sugar = new Sugar("加糖", dark);
                Console.WriteLine($"{sugar.GetDescription()}{sugar.Cost()}元");;
                var milk = new Milk("加奶", sugar);
                Console.WriteLine($"{milk.GetDescription()}{milk.Cost()}元");;
            }
        }