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
     */
     
     
     
  • 相关阅读:
    中国象棋评估函数建模
    C语言中指针变量传参
    STM32外部中断
    C语言中的注释
    STM32学习网址
    C语言中的布尔值
    更改KEIL背景配色
    Modbus通讯协议
    DUP
    算法的时间复杂度
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1809595.html
Copyright © 2011-2022 走看看