예제 #1
0
        static void Main()
        {
            Console.WriteLine("Decorator Pattern\n");

            IComponent component = new Component();

            Display("1. Basic component: ", component);
            Display("2. A-decorated: ", new DecoratorA(component));
            Display("3. B-decorated: ", new DecoratorB(component));
            Display("4. B-A-decorated: ", new DecoratorB(new DecoratorA(component)));

            DecoratorB b = new DecoratorB(new Component());

            Display("5. A-B-decorated: ", new DecoratorA(b));

            // invoking its added statt and added behavior
            Console.WriteLine("\t\t\t" + b.AddedState + b.AddedBehavior());

            using (var consoleDecorator = new ConsoleDecorator(Console.Out, 30))
            {
                Console.SetOut(consoleDecorator);
            }
            //Console.Write("C# 3.0 Design Patterns.");
            Console.WriteLine("Question No. 4: Decorate the Console class so that Write and WriteLine methods are trapped and the output is reformatted for lines of a given size, avoiding unsightly wrap-arounds. Test your decorator with the program in Example 2 - 1.");

            Console.ReadLine();
        }
예제 #2
0
        public void Test()
        {
            Tank t = new Tank50();

            Decorator d = new Decorator(t);

            d.Shot();
            d.Run();

            Decorator da = new DecoratorA(d);

            da.Shot();
            da.Run();

            Decorator db = new DecoratorB(da);

            db.Shot();
            db.Run();

            Decorator dc = new DecoratorC(db);

            dc.Shot();
            dc.Run();

            Console.ReadLine();
        }
예제 #3
0
        public static void Main(string[] args)
        {
            #region Prototype

            Component component = new ConcreteComponent();
            Decorator decorator = new DecoratorA();
            decorator.SetComponent(component);
            decorator.Operation();

            Decorator decorator1 = new DecoratorB();
            decorator1.SetComponent(decorator);
            decorator1.Operation();

            #endregion Prototype

            var person = new Person("小明");

            Console.WriteLine("\n第一种装扮:\n");
            var pants = new Pants();
            pants.Decorate(person);
            pants.Show();

            Console.WriteLine("\n第二种装扮:\n");
            var shirts = new Tshirts();
            shirts.Decorate(pants);
            shirts.Show();

            Console.ReadLine();
        }
예제 #4
0
        static void Main(string[] args)
        {
            ComponentA componentA = new ComponentA();
            DecoratorA decoratorA = new DecoratorA();
            DecoratorB decoratorB = new DecoratorB();

            decoratorA.SetComponent(componentA);
            decoratorB.SetComponent(decoratorA);
            decoratorB.Show();
        }
예제 #5
0
        /*
         *  装饰者模式:
         *  Component:定义ConcreteComponent和Decorator类要实现的方法,简单来说如果一个类继承于该类就具有装饰或被装饰能力。
         *  ConcreteComponent:让Decorator对象为自己添加功能。有时候使用ConcreteComponent的派生类提供核心功能,在这种情况就是用ConcreteComponent替代了Component的功能,而且装饰者是继承于ConcreteComponent的子类。
         *  Decorator:具有特定装饰功能的类,用来装饰ConcreteComponent类。
         */
        static void Main(string[] args)
        {
            Component component = new ConcreteComponent();

            component = new DecoratorA(component);
            component = new DecoratorB(component);

            component.Show();

            System.Console.ReadKey();
        }
예제 #6
0
        static void Main(string[] args)
        {
            IBilesen compnent = new Bilesen();

            Client.Display("1. basit bilesen: ", compnent);
            Client.Display("2. A Decorated: ", new DecoratorA(compnent));
            Client.Display("3. B Decorated: ", new DecoratorB(compnent));
            Client.Display("4. B-A Decorated: ", new DecoratorB(new DecoratorA(compnent)));

            DecoratorB b = new DecoratorB(new Bilesen());

            Client.Display("5. A-B Decorated", new DecoratorA(b));

            Console.WriteLine("\t\t\t" + b.EklenenDavranis());
            Console.ReadKey();
        }
예제 #7
0
        static void Main(string[] args)
        {
            Console.WriteLine("Decorator Pattern \n");
            IComponent component = new ComponentAA();

            Display("A: BasicPattern", component);
            Display("B: DecoratorAPattern", new DecoratorA(component));
            Display("C: DecoratorBPattern", new DecoratorB(component));
            Display("D: MixedPattern A>>B>> ", new DecoratorA(new DecoratorB(component)));
            Display("E: MixedPattern B>>A>>C ", new DecoratorB(new DecoratorA(component)));

            DecoratorB b = new DecoratorB(new ComponentAA());
            Console.WriteLine("F: " + new DecoratorA(b));
            Console.WriteLine(b.addBehavior());
            Console.WriteLine(b.addState);
        }
예제 #8
0
        static void Main(string[] args)
        {
            IBilesen component = new Bilesen();

            Client.Display("1. Basit bileşen: ", component);
            Client.Display("2. A-decorated : ", new DecoratorA(component));
            Client.Display("3. B-decorated : ", new DecoratorB(component));
            Client.Display("4. B-A-decorated : ", new DecoratorB(new DecoratorA(component)));

            DecoratorB b = new DecoratorB(new Bilesen());

            Client.Display("5. A-B-decorated : ", new DecoratorA(b));
            //Eklenen durmu ve davranışı çağırmak
            Console.WriteLine("\t\t\t" + b.addedState + b.EklenenDavranis());
            Console.ReadKey();
        }
예제 #9
0
        static void Main(string[] args)
        {
            IComponent orgComponent = new Component();
            DecoratorA aComponent   = new DecoratorA(orgComponent);
            DecoratorB bComponent   = new DecoratorB(orgComponent);
            DecoratorA abComponent  = new DecoratorA(bComponent);

            Console.WriteLine($"Original object: {orgComponent.Operation()}");

            Console.WriteLine($"A Decorator object: {aComponent.Operation()}. {aComponent.AddedBehavior()}");

            Console.WriteLine($"B Decorator object: {bComponent.Operation()}");

            Console.WriteLine($"AB Decorator object: {abComponent.Operation()}");

            Console.ReadKey();
        }
예제 #10
0
        /*
         * Intent
         * The intent of this pattern is to add additional responsibilities dynamically to an object.
         */
        static void Main(string[] args)
        {
            Console.WriteLine("Decorator Pattern\n");

            var component = new Component();

            Display("1. Basic component: ", component);
            Display("2. A-decorated : ", new DecoratorA(component));
            Display("3. B-decorated : ", new DecoratorB(component));
            Display("4. B-A-decorated : ", new DecoratorB(new DecoratorA(component)));

            // Explicit DecoratorB
            var b = new DecoratorB(new Component());

            Display("5. A-B-decorated : ", new DecoratorA(b));

            // Invoking its added state and added behavior
            Console.WriteLine($"\t\t\t{b.AddedState}{b.AddedBehavior()}");
        }