Пример #1
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);
        }
Пример #2
0
        public static void Test()
        {
            var concreteComponent = new ConcreteComponent("concrete property", "component property");
            concreteComponent.Operation();

            var concreteDecorator = new ConcreteDecorator(concreteComponent);
            concreteDecorator.AnotherOperation();
        }
Пример #3
0
        static void Main(string[] args)
        {
            // Decorators
            Component      simple         = new ConcreteComponent();
            DecoratorAlpha decoratorAlpha = new DecoratorAlpha(simple);
            DecoratorBeta  decoratorBeta  = new DecoratorBeta(decoratorAlpha);

            Console.WriteLine(simple.Operation());
            Console.WriteLine(decoratorAlpha.Operation());
            Console.Write(decoratorBeta.Operation());
        }
Пример #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
        /// <summary>
        /// Execute decorator design pattern example
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Decorator_OnClick(object sender, RoutedEventArgs e)
        {
            var simpleComponent = new ConcreteComponent();

            DesignPatternsLog.Text += $"Decorator@ I get a simple component: {simpleComponent.Operation()}";
            DesignPatternsLog.Text += $"{Environment.NewLine}";

            // Note how decorators can wrap not only simple components
            // but the other decorators as well.
            var decoratorA = new ConcreteDecoratorA(simpleComponent);
            var decoratorB = new ConcreteDecoratorB(decoratorA);

            DesignPatternsLog.Text += $"Decorator@ Now I've got a decorated component: {decoratorB.Operation()}";
            DesignPatternsLog.Text += $"{Environment.NewLine}";
        }
Пример #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();
    }