コード例 #1
0
        public void test_component_demo()
        {
            ConcreteComponentA componentA = new ConcreteComponentA();
            ConcreteComponentB componentB = new ConcreteComponentB();

            ConcreteDecoratorA decoratorA = new ConcreteDecoratorA(componentA);

            decoratorA.Operation();

            decoratorA.SetComponent(componentB);
            decoratorA.Operation();
        }
コード例 #2
0
        static bool TestDecoratorPattern()
        {
            Console.WriteLine("TESTING THE DECORATOR DESIGN PATTERN: ");

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

            comp.Operation();
            //Output:
            //Operation from ConcreteComponent

            d1.Operation();
            //Output:
            //Operation from ConcreteComponent
            //(AddedBehaviour A) + Operation from ConcreteDecoratorA

            d2.Operation();
            //Output:
            //Operation from ConcreteComponent
            //(AddedBehaviour A) + Operation from ConcreteDecoratorA
            //(AddedBehaviour B) + Operation from ConcreteDecoratorB

            return(true);
        }
コード例 #3
0
        public void AddStateTest()
        {
            Component component = new ConcreteComponent();

            component = new ConcreteDecoratorA(component);

            Assert.That((component as ConcreteDecoratorA)?.AddedState == "added state");
            Assert.That(() => component.Operation(),
                        Throws.Exception.With.Message.EqualTo("Operation from object of ConcreteComponent type"));
        }
コード例 #4
0
        public static void TestDecorator()
        {
            IComponent component = new ConcreteComponent();

            Console.WriteLine(component.Operation());
            Decorator decorator = new ConcreteDecoratorA(component);

            Console.WriteLine(decorator.Operation());
            decorator = new ConcreteDecoratorB(decorator);
            Console.WriteLine(decorator.Operation());
        }
コード例 #5
0
        public static void test()
        {
            // Create ConcreteComponent and two Decorators
            ConcreteComponent c  = new ConcreteComponent("c");
            Decorator         a1 = new ConcreteDecoratorA("a1");
            Decorator         b1 = new ConcreteDecoratorA("b1");
            Decorator         a2 = new ConcreteDecoratorA("a2");

            // Link decorators
            a1.SetComponent(c);
            b1.SetComponent(a1);
            a2.SetComponent(b1);

            a2.Operation();
        }
コード例 #6
0
    public static void Main()
    {
        ConcreteComponent cc = new ConcreteComponent();

        cc.Operation();
        Console.WriteLine();

        ConcreteDecoratorA cd1 = new ConcreteDecoratorA(cc);

        cd1.Operation();
        Console.WriteLine();

        ConcreteDecoratorB cd2 = new ConcreteDecoratorB(cd1);

        cd2.Operation();
        Console.WriteLine();

        Console.ReadKey();
    }