Esempio n. 1
0
        //https://ru.wikipedia.org/wiki/Декоратор_(шаблон_проектирования)
        //https://refactoring.guru/ru/design-patterns/decorator
        //https://metanit.com/sharp/patterns/4.1.php

        static void Main(string[] args)
        {
            // Create ConcreteComponent and two Decorators
            ConcreteComponent  c  = new ConcreteComponent();
            ConcreteDecoratorA dA = new ConcreteDecoratorA();
            ConcreteDecoratorB dB = new ConcreteDecoratorB();

            // Link decorators
            dA.SetComponent(c);
            dB.SetComponent(dA);

            dA.Operation();

            dB.Operation();

            //OnInterface
            Implement           implement = new Implement();
            ConcreteDecoratorAI dAI       = new ConcreteDecoratorAI();
            ConcreteDecoratorBI dBI       = new ConcreteDecoratorBI();

            // Link decorators
            dAI.SetComponent(implement);
            dBI.SetComponent(dAI);

            implement.Operation();

            dAI.Operation();

            dBI.Operation();

            // Wait for user
            Console.Read();
        }
Esempio n. 2
0
        static void Main(string[] args)
        {
            ConcreteComponent  c  = new ConcreteComponent();
            ConcreteDecoratorA d1 = new ConcreteDecoratorA();
            ConcreteDecoratorB d2 = new ConcreteDecoratorB();

            d1.SetComponent(c);
            d2.SetComponent(d1);
            d2.Operation();
            Console.ReadKey();
        }
Esempio n. 3
0
        private static void Main()
        {
            var component          = new ConcreteComponent();
            var concreteDecoratorA = new ConcreteDecoratorA();
            var concreteDecoratorB = new ConcreteDecoratorB();

            concreteDecoratorA.SetComponent(component);
            concreteDecoratorB.SetComponent(concreteDecoratorA);
            concreteDecoratorB.Operation();

            Console.ReadKey();
        }
Esempio n. 4
0
        private static void NewMethod2()
        {
            ConcreteComponent  c  = new ConcreteComponent();
            ConcreteDecoratorA d1 = new ConcreteDecoratorA();
            ConcreteDecoratorB d2 = new ConcreteDecoratorB();

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

            Console.Read();
        }
Esempio n. 5
0
        static void Main()
        {
            ConcreteComponent c = new ConcreteComponent();
            ConcreteDecoratorA dA = new ConcreteDecoratorA();
            ConcreteDecoratorB dB = new ConcreteDecoratorB();

            dA.SetComponent(c);
            dB.SetComponent(dA);

            dB.Operation();

            Console.Read();
        }
Esempio n. 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();
            // Wait for user
            Console.ReadKey();
        }
        static void Main()
        {
            ConcreteComponent  cmp = new ConcreteComponent();
            ConcreteDecoratorA d1  = new ConcreteDecoratorA();
            ConcreteDecoratorB d2  = new ConcreteDecoratorB();

            d1.SetComponent(cmp);
            d2.SetComponent(cmp);

            d1.Operation();
            d2.Operation();

            Console.ReadKey();
        }
        /// <summary>
        /// The test first decorator.
        /// </summary>
        private static void TestFirstDecorator()
        {
            // 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.ReadKey();
        }
Esempio n. 9
0
        /// <summary>
        /// Entry point into console application.
        /// </summary>
        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.ReadKey();
        }
Esempio n. 10
0
    { /// <summary>
      /// Entry point into console application.
      /// </summary>
        static void Main()
        {
            // Create ConcreteComponent and two Decorators
            var c  = new ConcreteComponent();
            var d1 = new ConcreteDecoratorA();
            var d2 = new ConcreteDecoratorB();

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

            d2.Operation();

            // Wait for user
            Console.ReadKey();
        }
Esempio n. 11
0
        static void Main()
        {
            ConcreteComponent  c  = new ConcreteComponent();
            ConcreteDecoratorA dA = new ConcreteDecoratorA();
            ConcreteDecoratorB dB = new ConcreteDecoratorB();

            dA.SetComponent(c);
            dB.SetComponent(dA);

            dA.Operation();

            Console.WriteLine();

            dB.Operation();

            Console.Read();
        }
Esempio n. 12
0
        static void Main()
        {
            ConcreteComponent
                c = new ConcreteComponent();

            ConcreteDecoratorA
                d1 = new ConcreteDecoratorA();

            ConcreteDecoratorB
                d2 = new ConcreteDecoratorB();

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

            d2.Operation();
        }
Esempio n. 13
0
        static void Main(string[] args)
        {
            ConcreteComponent concreteComponent = new ConcreteComponent();

            concreteComponent.Operation();

            ConcreteDecoratorA decoratorA = new ConcreteDecoratorA();

            decoratorA.SetComponent(concreteComponent);
            ConcreteDecoratorB decoratorB = new ConcreteDecoratorB();

            decoratorB.SetComponent(concreteComponent);

            decoratorA.Operation();
            decoratorB.Operation();


            Console.WriteLine("-----------------------------");

            Repo repo = new Repo();

            repo.GetId();

            DecoratorForIRepo decoratorForIRepo = new DecoratorForIRepo(repo);

            decoratorForIRepo.GetId();

            Console.WriteLine("-----------------------------");

            new CofeeHelper().ShouldSupportCondiments();


            Console.WriteLine("-----------------------------");


            BaseCar baseCar = new BaseCar();

            Console.WriteLine(baseCar.GetCarCharacteristic());
            Console.WriteLine(baseCar.GetCost());

            ICarSet superCar = new AutomaticTransmissionSet(baseCar);

            Console.WriteLine(superCar.GetCarCharacteristic());
            Console.WriteLine(superCar.GetCost());
        }
Esempio n. 14
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            // Decorator
            ConcreteComponent  c  = new ConcreteComponent();
            ConcreteDecoratorA d1 = new ConcreteDecoratorA();
            ConcreteDecoratorB d2 = new ConcreteDecoratorB();

            // 先用ConcreteComponent实例化对象c
            // 然后用ConcreteComponentA的实例化对象d1包装c
            d1.SetComponent(c);
            // 再用ConcreteComponentB的实例化对象d2包装d1
            d2.SetComponent(d1);
            //最终执行d2的Operation()
            d2.Operation();

            Console.Read();
        }
        static void Main(string[] args)
        {
            // Se crea ConcreteComponent y 2 Decoradores

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

            // Se liga los decoradores

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

            d2.Operation();

            // Espera al usuario

            Console.ReadKey();
        }
Esempio n. 16
0
        public static void Run()
        {
            Console.WriteLine("This structural code demonstrates the Decorator pattern which dynamically adds extra functionality to an existing object.");

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

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

            d2.Operation();

            /*
             * ConcreteComponent.Operation()
             * ConcreteDecoratorA.Operation()
             * ConcreteDecoratorB.Operation()
             */
        }
Esempio n. 17
0
        static void Main()
        {
            /*
             * Structural
             */

            // Create ConcreteComponent and two Decorators
            StructuralConcreteComponent c  = new StructuralConcreteComponent();
            ConcreteDecoratorA          d1 = new ConcreteDecoratorA();
            ConcreteDecoratorB          d2 = new ConcreteDecoratorB();

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

            // Call decorators operation
            d2.Operation();

            /*
             * Real World
             */

            // Create a book
            Book book = new Book("Cantu, Jeremy", "My Life as a Person", 12345);

            book.Display();

            // Create a video
            Video video = new Video("Tarantino, Quentin", "Jeremy's Life", 54321, 60);

            video.Display();

            // Make video borrow-able, then borrow and display
            Console.WriteLine("Making video borrow-able");

            Borrowable borrowableVideo = new Borrowable(video);

            borrowableVideo.BorrowItem("Customer #1");
            borrowableVideo.BorrowItem("Customer #2");

            borrowableVideo.Display();
        }
Esempio n. 18
0
        static void Main()
        {
            // Create ConcreteComponent and two Decorators
            ConcreteComponent c = new ConcreteComponent();
            ConcreteDecoratorA dA = new ConcreteDecoratorA();
            ConcreteDecoratorB dB = new ConcreteDecoratorB();

            // Link decorators
            dA.SetComponent(c);
            dB.SetComponent(dA);

            dA.Operation();

            Console.WriteLine();

            dB.Operation();

            // Wait for user
            Console.Read();
        }
Esempio n. 19
0
        static void Main()
        {
            // Create ConcreteComponent and two Decorators
            ConcreteComponent  c  = new ConcreteComponent();
            ConcreteDecoratorA dA = new ConcreteDecoratorA();
            ConcreteDecoratorB dB = new ConcreteDecoratorB();

            // Link decorators
            dA.SetComponent(c);
            dB.SetComponent(dA);

            dA.Operation();

            Console.WriteLine();

            dB.Operation();

            // Wait for user
            Console.Read();
        }
Esempio n. 20
0
        static void Main(string[] args)
        {
            #region 基本用法(对应Basic.cs)
            ConcreteComponent  c  = new ConcreteComponent();
            ConcreteDecoratorA d1 = new ConcreteDecoratorA();
            ConcreteDecoratorB d2 = new ConcreteDecoratorB();

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

            d2.Operation();
            #endregion

            #region 具体实例(对应Example.cs)
            Person xc = new Person("小菜");
            Console.WriteLine("\n第一种装扮:");

            Sneaker    pqx = new Sneaker();
            BigTrouser kk  = new BigTrouser();
            TShirt     dtx = new TShirt();

            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.ReadLine();
            #endregion
        }
Esempio n. 21
0
        static void Main(string[] args)
        {
            #region 基本用法(对应Basic.cs)
            ConcreteComponent c = new ConcreteComponent();
            ConcreteDecoratorA d1 = new ConcreteDecoratorA();
            ConcreteDecoratorB d2 = new ConcreteDecoratorB();

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

            d2.Operation();
            #endregion

            #region 具体实例(对应Example.cs)
            Person xc = new Person("小菜");
            Console.WriteLine("\n第一种装扮:");

            Sneaker pqx = new Sneaker();
            BigTrouser kk = new BigTrouser();
            TShirt dtx = new TShirt();

            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.ReadLine();
            #endregion
        }
Esempio n. 22
0
        static void Main(string[] args)
        {
            #region 结构实现
            // 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();
            #endregion

            Console.WriteLine("******************************");

            #region 实践应用
            #endregion

            Console.ReadKey();
        }
Esempio n. 23
0
        static void Main(string[] args)
        {
            var c  = new ConcreteComponent();
            var d1 = new ConcreteDecoratorA();
            var d2 = new ConcreteDecoratorB();

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

            //範例
            Person xc = new Person("小菜");

            Console.WriteLine("第一種裝扮");
            var kk  = new BigTrouser();
            var dtx = new TShirts();

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

            Console.ReadLine();
        }
            static void Main()
            {
                // Конкретный компонент  и 3 Конкретных декоратора
                ConcreteComponent  c   = new ConcreteComponent();
                ConcreteDecoratorA dA  = new ConcreteDecoratorA();
                ConcreteDecoratorB dB  = new ConcreteDecoratorB();
                ConcreteDecoratorB dB2 = new ConcreteDecoratorB();

                dA.SetComponent(c);   //оборачиваем с в первый декоратор
                dB.SetComponent(c);
                dB2.SetComponent(dA); //оборачиваем dA во второй декоратор

                dA.Operation();

                Console.WriteLine();

                dB.Operation();

                Console.WriteLine();

                dB2.Operation();

                Console.Read();
            }
Esempio n. 25
0
        static void Main(string[] args)
        {
            Person person = new Person("小菜");

            Console.WriteLine("第一種裝扮:");
            Finery tShirts    = new TShirts();
            Finery bigTrouser = new BigTrouser();
            Finery sneakers   = new Sneakers();

            sneakers.Decorate(person);
            bigTrouser.Decorate(sneakers);
            tShirts.Decorate(bigTrouser);
            tShirts.Show();

            Console.WriteLine("\n第二種裝扮:");
            Finery suit         = new Suit();
            Finery tie          = new Tie();
            Finery leatherShoes = new LeatherShoes();

            suit.Decorate(person);
            tie.Decorate(suit);
            leatherShoes.Decorate(tie);
            leatherShoes.Show();

            Console.WriteLine("\n");

            Component component  = new ConcreteComponent();
            Decorator decoratorA = new ConcreteDecoratorA();
            Decorator decoratorB = new ConcreteDecoratorB();

            decoratorA.SetComponent(component);
            decoratorB.SetComponent(decoratorA);
            decoratorB.Operation();

            Console.ReadLine();
        }