mon image

décorateurs Csharp

Yves Guidet (yves.guidet@gmail.com)

28/05/2017

résultat des courses

       1	    
       2	    
       3	     using System;
       4	    
       5	     namespace DoFactory.GangOfFour.Decorator.Structural
       6	     {
       7	       /// <summary>
       8	       /// MainApp startup class for Structural
       9	       /// Decorator Design Pattern.
      10	      /// </summary>
      11	      class MainApp
      12	      {
      13	        /// <summary>
      14	        /// Entry point into console application.
      15	        /// </summary>
      16	        static void Main()
      17	        {
      18	          // Create ConcreteComponent and two Decorators
      19	          ConcreteComponent c = new ConcreteComponent();
      20	          ConcreteDecoratorA d1 = new ConcreteDecoratorA();
      21	          ConcreteDecoratorB d2 = new ConcreteDecoratorB();
      22	   
      23	          // Link decorators
      24	          d1.SetComponent(c);
      25	          d2.SetComponent(d1);
      26	   
      27	          d2.Operation();
      28	   
      29	          // Wait for user
      30	          Console.ReadKey();
      31	        }
      32	      }
      33	   
      34	      /// <summary>
      35	      /// The 'Component' abstract class
      36	      /// </summary>
      37	      abstract class Component
      38	      {
      39	        public abstract void Operation();
      40	      }
      41	   
      42	      /// <summary>
      43	      /// The 'ConcreteComponent' class
      44	      /// </summary>
      45	      class ConcreteComponent : Component
      46	      {
      47	        public override void Operation()
      48	        {
      49	          Console.WriteLine("ConcreteComponent.Operation()");
      50	        }
      51	      }
      52	   
      53	      /// <summary>
      54	      /// The 'Decorator' abstract class
      55	      /// </summary>
      56	      abstract class Decorator : Component
      57	      {
      58	        protected Component component;
      59	   
      60	        public void SetComponent(Component component)
      61	        {
      62	          this.component = component;
      63	        }
      64	   
      65	        public override void Operation()
      66	        {
      67	          if (component != null)
      68	          {
      69	            component.Operation();
      70	          }
      71	        }
      72	      }
      73	   
      74	      /// <summary>
      75	      /// The 'ConcreteDecoratorA' class
      76	      /// </summary>
      77	      class ConcreteDecoratorA : Decorator
      78	      {
      79	        public override void Operation()
      80	        {
      81	          base.Operation();
      82	          Console.WriteLine("ConcreteDecoratorA.Operation()");
      83	        }
      84	      }
      85	   
      86	      /// <summary>
      87	      /// The 'ConcreteDecoratorB' class
      88	      /// </summary>
      89	      class ConcreteDecoratorB : Decorator
      90	      {
      91	        public override void Operation()
      92	        {
      93	          base.Operation();
      94	          AddedBehavior();
      95	          Console.WriteLine("ConcreteDecoratorB.Operation()");
      96	        }
      97	   
      98	        void AddedBehavior()
      99	        {
     100	        }
     101	      }
     102	    }
     103	   
     104	   
     105	   
     106	   
         
     107	//Output
     108	//
     109	//   ConcreteComponeOperation()
     110	//   ConcreteDecoratoOperation()
     111	//   ConcreteDecoratoOperation()

retour À SUIVRE



Retour à la page d'accueil

© Yves Guidet jeudi 9 juillet 2020, 09:48:17 (UTC+0200) (bella ubuntu, site printemps) yves.guidet@gmail.com