我讀設計模式之命令模式

來源:互聯網
上載者:User
using System;   
  
///1.容易設計命令隊列   
///2.需要的情況下,可以很容易的將命令記入日誌   
///3.很容易的實現命令的撤銷和重做   
///   
/// 命令模式:   
/// 將請求一個操作的對象與知道怎麼執行一個操作的對象分割開。   
/// 註:一般的方法調用方法,是直接通過對象調用或者類調用,這樣的調用方式,   
/// 行為要求者和行為實現者之間的是緊耦合的關係即 A和B的調用關係)   
/// 比如你需要對系列的動作調用實現記錄,撤銷,重做等操作的時候,   
/// 就需要對這些行為要求者和行為實現者之間實現解耦。   
/// 那麼command模式給你一個思路:抽象,引入第三者,實現解耦。   
/// 這也是許多設計模式的一個比較通行的解耦思路。   
///    
  
namespace CommandPattern   
{   
    class Program   
    {   
        static void Main(string[] args)   
        {   
            Document doc = new Document();   
  
            Command doCmd = new DoCommand(doc);   
            Command unDoCmd = new UnDoCommand(doc);   
            Command reDoCmd = new ReDoCommand(doc);   
  
            CmdInvoke invoke = new CmdInvoke();   
  
            invoke.AddCmd(doCmd);   
            invoke.AddCmd(unDoCmd);   
            invoke.AddCmd(reDoCmd);   
  
            invoke.Execute();   
  
            Console.Read();   
        }   
    }   
  
    public abstract class Command   
    {   
        public Document doc;   
        public Command(Document doc)   
        {   
            this.doc = doc;   
        }   
  
        public abstract void ExecuteCommand();   
    }   
  
    public class DoCommand : Command   
    {   
        public DoCommand(Document doc) : base(doc) { }   
  
        public override void ExecuteCommand()   
        {   
            doc.Do();   
        }   
    }   
  
    public class UnDoCommand : Command   
    {   
        public UnDoCommand(Document doc) : base(doc) { }   
  
        public override void ExecuteCommand()   
        {   
            doc.UnDo();   
        }   
    }   
  
    public class ReDoCommand : Command   
    {   
        public ReDoCommand(Document doc) : base(doc) { }   
  
        public override void ExecuteCommand()   
        {   
            doc.ReDo();   
        }   
    }   
  
    public class CmdInvoke   
    {   
        List<Command> cmdList = new List<Command>();   
  
        public void AddCmd(Command cmd)   
        {   
            cmdList.Add(cmd);   
        }   
  
        public void UndoCmd(Command cmd)   
        {   
            cmdList.Remove(cmd);   
        }   
  
        public void Execute()   
        {   
            foreach (Command cmd in cmdList)   
            {   
                cmd.ExecuteCommand();   
            }   
        }   
    }   
  
  
    public class Document   
    {   
        public string info { set; get; }   
  
        public void Do()   
        {   
            Console.WriteLine("Do Something");   
        }   
  
        public void UnDo()   
        {   
            Console.WriteLine("UnDo something");   
        }   
  
        public void ReDo()   
        {   
            Console.WriteLine("Redo Something");   
        }   
    }   
}  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.