zoukankan      html  css  js  c++  java
  • decoratortheory.cs

      using System;
     
      //Decorator Pattern      Judith Bishop Dec 2006
      // Shows two decorators and the output of various
      // combinations of the decorators on the basic component
     
      interface IComponent {
        string Operation();
      }

      class Component  : IComponent {
        public string Operation () {
          return "I am walking ";
        }
      }

      class DecoratorA: IComponent {
        IComponent component;

        public DecoratorA (IComponent c) {
           component = c;
        }

        public string Operation() {
          string s = component.Operation();
          s += "and listening to Classic FM ";
          return s;
        }
      }

      class DecoratorB  : IComponent {
        IComponent component;
        public string addedState = "past the Coffee Shop ";

        public DecoratorB (IComponent c) {
          component = c;
        }

        public string Operation () {
          string s = component.Operation ();
          s += "to school ";
           return s;
        }

        public string AddedBehavior() {
          return "and I bought a cappucino ";
        }
      }

      class Client {
       
        static void Display(string s, IComponent c) {
          Console.WriteLine(s+ c.Operation());
        }

        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)));
          // Explicit DecoratorB
          DecoratorB b = new DecoratorB(new Component());
          Display("5. A-B-decorated : ", new DecoratorA(b));
          //Invoking its added state and added behaviour
          Console.WriteLine("\t\t\t"+ b.addedState + b.AddedBehavior());
        }
      }

    /* Output:
    Decorator Pattern

    1. Basic component: I am walking
    2. A-decorated : I am walking and listening to Classic FM
    3. B-decorated : I am walking to school
    4. B-A-decorated : I am walking and listening to Classic FM to school
    5. A-B-decorated : I am walking to school and listening to Classic FM
          past the Coffee Shop and I bought a cappucino
    */
  • 相关阅读:
    LED点阵书写显示
    cpld fpga 区别
    SublimeText 自带格式化代码功能
    iText C# 合并PDF文件流,以及A5变A4时内容默认放在最底下的问题的解决方法;ASP.NET 实现Base64文件流下载PDF
    Adobe Acrobat 9 Pro序列号
    c#比较两个数组的差异
    iNotify.js通知JS 实现浏览器的 title 闪烁、滚动、声音提示、chrome、Firefox、Safari等系统通知。
    配置IISExpress允许外部访问
    https://sweetalert2.github.io/
    c# 利用MailKit.IMap 收取163邮件
  • 原文地址:https://www.cnblogs.com/shihao/p/2495995.html
Copyright © 2011-2022 走看看