示例#1
0
            static void Main()
            {
                //这就是我们的饺子馅,需要装饰的房子
                House myselfHouse = new PatrickLiuHouse();

                DecorationStrategy securityHouse = new HouseSecurityDecorator(myselfHouse);

                securityHouse.Renovation();
                //房子就有了安全系统了

                //如果我既要安全系统又要保暖呢,继续装饰就行
                DecorationStrategy securityAndWarmHouse = new HouseSecurityDecorator(securityHouse);

                securityAndWarmHouse.Renovation();
            }
示例#2
0
        //装饰者模式调用
        public static void GetDecorator()
        {
            //这就是我们的饺子馅,需要装饰的房子
            House myselfhouse = new PatrickLiuHouse();
            //房子有安全系统了
            DecorationStrategy securityHouse = new HouseSecurityDecorator(myselfhouse);

            securityHouse.Renovation();

            //房子有保暖系统了
            DecorationStrategy WarmHouse = new KeepWarmDecorator(myselfhouse);

            WarmHouse.Renovation();

            //如果我既要安全系统又要保暖呢,继续装饰就行
            DecorationStrategy securityAndWarmHouse = new KeepWarmDecorator(securityHouse);

            securityAndWarmHouse.Renovation();
        }
        public void DecoratorPatternTest01()
        {
            // 需要装饰的房子
            var house = new House();

            BaseDecorator securityHouse = new HouseSecurityDecorator(house);

            // 房子就有了安全系统了
            securityHouse.Renovation();

            Assert.AreEqual(house.Content[0], "装修房子");
            Assert.AreEqual(house.Content[1], "增加安全系统");

            // 如果我既要安全系统又要保暖呢,继续装饰就行
            BaseDecorator securityAndWarmHouse = new KeepWarmDecorator(securityHouse);

            securityAndWarmHouse.Renovation();

            Assert.AreEqual(house.Content[0], "装修房子");
            Assert.AreEqual(house.Content[1], "增加安全系统");
            Assert.AreEqual(house.Content[2], "增加保温的功能");
        }