Beispiel #1
0
 static void Main(string[] args)
 {
     IComponent component = new ConcreteComponent();
     ConcreteDecoratorA decoratorA = new ConcreteDecoratorA(component);
     ConcreteDecoratorB decoratorB = new ConcreteDecoratorB(decoratorA);
     decoratorB.Operation();
 }
        static void Main1(string[] args)
        {
            ConcreteComponent  c  = new ConcreteComponent();
            ConcreteDecoratorA d1 = new ConcreteDecoratorA();
            ConcreteDecoratorB d2 = new ConcreteDecoratorB();

            d1.SetComponent(c);
            d2.SetComponent(d1);
            d2.Operation();

            Console.ReadKey();
        }
        /// <summary>
        /// The Decorator Design Pattern aims to add functionality to existing classes. This is done by implementing a decorator base-class, that holds the classes to be decorated and executes its methods, thus enabling a chain of calls.
        /// </summary>
        /// <example>
        /// Real life uses include...
        ///     - adding filters-criterias to filter a data-set
        ///     - adding calculations to models - fx meteological maps
        ///     - decorating a command with others, thus creating a chain of commands to be executed
        /// </example>
        private static void Main()
        {
            // Create ConcreteComponent and two Decorators
            ConcreteComponent  c  = new ConcreteComponent();
            ConcreteDecoratorA d1 = new ConcreteDecoratorA(c);
            ConcreteDecoratorB d2 = new ConcreteDecoratorB(d1);

            d2.Operation();

            // Wait for user
            Console.ReadKey();
        }
Beispiel #4
0
        static void Main(string[] args)
        {
            ConcreteComponent  cComponent = new ConcreteComponent();
            ConcreteDecoratorA cdA        = new ConcreteDecoratorA();
            ConcreteDecoratorB cdB        = new ConcreteDecoratorB();

            cdA.SetComponent(cComponent);
            cdB.SetComponent(cdA);
            cdB.Operation();

            Console.ReadLine();
        }
        static void Main(string[] args)
        {
            ConcreteComponent  component  = new ConcreteComponent();
            ConcreteDecoratorA decoratorA = new ConcreteDecoratorA();
            ConcreteDecoratorB decoratorB = new ConcreteDecoratorB();

            decoratorA.Component = component;
            decoratorB.Component = decoratorA;

            decoratorB.Operation();
            Console.Read();
        }
Beispiel #6
0
        static void Main(string[] args)
        {
            // Create ConcreteComponent and two Decorators
            ConcreteComponent  c  = new ConcreteComponent();
            ConcreteDecoratorA d1 = new ConcreteDecoratorA();
            ConcreteDecoratorB d2 = new ConcreteDecoratorB();

            // Link decorators
            d1.SetComponent(c);
            d2.SetComponent(d1);

            d2.Operation();
        }
        static void Main(string[] args)
        {
            ConcreteComponent concreteComponent = new ConcreteComponent();
            ConcreteDecoratorA concreteDecoratorA = new ConcreteDecoratorA();
            ConcreteDecoratorB concreteDecoratorB = new ConcreteDecoratorB();

            concreteDecoratorA.Component = concreteComponent;
            concreteDecoratorB.Component = concreteDecoratorA;

            concreteDecoratorB.Operation();

            Console.Read();
        }
Beispiel #8
0
        private static void Main(string[] args)
        {
            var c = new ConcreteComponent();
            var a = new ConcreteDecoratorA();
            var b = new ConcreteDecoratorB();

            c.Operation();

            a.SetComponent(c);
            a.Operation();

            b.SetComponent(a);
            b.Operation();

            Console.ReadKey();
        }
Beispiel #9
0
        static void Main()
        {
            // Create ConcreteComponent and two Decorators
            ConcreteComponent  component  = new ConcreteComponent();
            ConcreteDecoratorA decorator1 = new ConcreteDecoratorA();
            ConcreteDecoratorB decorator2 = new ConcreteDecoratorB();

            // Link decorators
            decorator1.SetComponent(component);
            decorator2.SetComponent(decorator1);

            decorator2.Operation();

            // Wait for user
            Console.ReadKey();
        }
Beispiel #10
0
        private static void Main(string[] args)
        {
            var c = new ConcreteComponent();
            var a = new ConcreteDecoratorA();
            var b = new ConcreteDecoratorB();

            c.Operation();

            a.SetComponent(c);
            a.Operation();

            b.SetComponent(a);
            b.Operation();

            Console.ReadKey();
        }
        static void Main()
        {
            // Create ConcreteComponent and two Decorators
            ConcreteComponent c = new ConcreteComponent();
            ConcreteDecoratorA d1 = new ConcreteDecoratorA();
            ConcreteDecoratorB d2 = new ConcreteDecoratorB();

            // Link decorators
            d1.SetComponent(c);
            d2.SetComponent(d1);

            d2.Operation();

            // Wait for user
            Console.Read();
        }
Beispiel #12
0
        static void Main(string[] args)
        {
            try
            {
                {//BasicStructure
                    ConcreteComponent  c  = new ConcreteComponent();
                    ConcreteDecoratorA d1 = new ConcreteDecoratorA();
                    ConcreteDecoratorB d2 = new ConcreteDecoratorB();

                    d1.SetComponent(c);
                    d2.SetComponent(d1);

                    d2.Operation();
                }
                {//SituationSimulation
                    //第一种写法
                    Console.WriteLine("***************第一种写法:***************");
                    AppleHandset   handset        = new AppleHandset();
                    HandsetSticker handsetSticker = new HandsetSticker(handset);
                    HandsetCasing  handsetCasing  = new HandsetCasing(handsetSticker);
                    handsetCasing.Operation();

                    //第二种写法
                    Console.WriteLine("***************第二种写法:***************");
                    Handset appleHandset = new AppleHandset();
                    appleHandset = new HandsetSticker(appleHandset);
                    appleHandset = new HandsetCasing(appleHandset);

                    appleHandset.Operation();
                    //第二种写法的简化
                    Console.WriteLine("***************第二种写法的简化:***************");
                    new HandsetCasing(new HandsetSticker(new AppleHandset())).Operation();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadKey();
        }
Beispiel #13
0
        static void Main(string[] args)
        {
            try
            {
                //介绍
                //    意图:动态地给一个对象添加一些额外的职责。就增加功能来说,装饰器模式相比生成子类更为灵活。

                //主要解决:一般的,我们为了扩展一个类经常使用继承方式实现,由于继承为类引入静态特征,并且随着扩展功能的增多,子类会很膨胀。

                //何时使用:在不想增加很多子类的情况下扩展类。

                //如何解决:将具体功能职责划分,同时继承装饰者模式。

                //关键代码: 1、Component 类充当抽象角色,不应该具体实现。 2、修饰类引用和继承 Component 类,具体扩展类重写父类方法。

                //应用实例: 1、孙悟空有 72 变,当他变成"庙宇"后,他的根本还是一只猴子,但是他又有了庙宇的功能。 2、不论一幅画有没有画框都可以挂在墙上,但是通常都是有画框的,并且实际上是画框被挂在墙上。在挂在墙上之前,画可以被蒙上玻璃,装到框子里;这时画、玻璃和画框形成了一个物体。

                //优点:装饰类和被装饰类可以独立发展,不会相互耦合,装饰模式是继承的一个替代模式,装饰模式可以动态扩展一个实现类的功能。

                //缺点:多层装饰比较复杂。

                //使用场景: 1、扩展一个类的功能。 2、动态增加功能,动态撤销。

                //注意事项:可代替继承。

                //概念:1.动态地给一个对象添加一个额外的职责,就增加功能来说,装饰模式比生成子类更加灵活、
                //      2.装饰器模式是利用SetComponent来对对象进行包装的,这样每个装饰对象的实现就和如何使用这个对象分离开了,每个装饰对象只关心自己的功能,不需要关心如何被添加到对象链当中(DPE)
                #region 装饰模式

                ConcreteComponent  c  = new ConcreteComponent();
                ConcreteDecoratorA d1 = new ConcreteDecoratorA();
                ConcreteDecoratorB d2 = new ConcreteDecoratorB();

                d1.SetComponent(c);
                d2.SetComponent(d1);

                d2.Operation();


                Person xc = new Person("小菜");

                Console.WriteLine("\n第一种装扮:");

                Sneakers   pqx = new Sneakers();
                BigTrouser kk  = new BigTrouser();
                TShirts    dtx = new TShirts();

                pqx.Decorate(xc);
                kk.Decorate(pqx);
                dtx.Decorate(kk);
                dtx.Show();

                Console.WriteLine("\n第二种装扮:");

                LeatherShoes px = new LeatherShoes();
                Tie          ld = new Tie();
                Suit         xz = new Suit();

                px.Decorate(xc);
                ld.Decorate(px);
                xz.Decorate(ld);
                xz.Show();

                Console.WriteLine("\n第三种装扮:");
                Sneakers     pqx2 = new Sneakers();
                LeatherShoes px2  = new LeatherShoes();
                BigTrouser   kk2  = new BigTrouser();
                Tie          ld2  = new Tie();

                pqx2.Decorate(xc);
                px2.Decorate(pqx);
                kk2.Decorate(px2);
                ld2.Decorate(kk2);

                ld2.Show();


                #endregion
            }

            catch (Exception e)
            {
                Console.WriteLine($"遇到异常,ErrorMsg:{e.Message}");
                Console.ReadLine();
            }
        }