zoukankan      html  css  js  c++  java
  • 命令模式(Command Pattern)


      using System;
     
       class CommandPattern {
     
         // Command Pattern           Judith Bishop June 2007
         //
         // Uses a single delegate for the single type of commands that the client invokes.
     
         delegate void Invoker ();
         static Invoker Execute, Undo, Redo;
       
         class Command  {
           public Command(Receiver receiver) {  
             Execute = receiver.Action;
             Redo = receiver.Action;
             Undo = receiver.Reverse;
           }
         }
       
         public class Receiver {
           string build, oldbuild;
           string s = "some string ";
         
           public void Action() {
               oldbuild = build;
               build +=s;
               Console.WriteLine("Receiver is adding "+build);
           }
          
           public void Reverse() {
             build = oldbuild;
             Console.WriteLine("Receiver is reverting to "+build);
           }
         }
       
         static void Main() {
           new Command (new Receiver());
           Execute();
           Redo();
           Undo();
           Execute();
           Console.ReadKey();
         }
       }
      /* Output
     Receiver is adding some string 
     Receiver is adding some string some string 
     Receiver is reverting to some string 
     Receiver is adding some string some string
     */
     
     
     
  • 相关阅读:
    Android AdapterView View的复用机制 分析
    go12---interface
    go11---方法method
    go10---struct
    go09---defer
    go8---函数function
    go7---map
    go6---slice切片
    go5--数组
    go4--break,continue + 标签
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1809595.html
Copyright © 2011-2022 走看看