命令模式:
命令模式又称为行动(Action)模式或交易(Transaction)模式。命令模式把一个请求或者操作封装到一个对象中。命令模式允许系统使用不同的请求把客户端参数化,对请求排队或者记录请求日志,可以提供命令的撤销和恢复功能。
命令模式是对命令的封装。命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象。
每一个命令都是一个操作:请求的一方发出请求要求执行一个操作;接收的一方收到请求,并执行操作。命令模式允许请求的一方和接收的一方独立开来,使得请求的一方不必知道接收请求的一方的接口,更不必知道请求是怎么被接收,以及操作是否被执行、何时被执行,以及是怎么被执行的。
UML:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CommandPattern { class Message { private int id; private string name; public int Id { get { return id; } set { id = value; } } public string Name { set { name = value; } get { return name; } } public Message(int id,string name) { this.id = id; this.name = name; } } public enum Action { Delete, Insert } public interface ICommand { void Excute(); void UnExcute(); } class ConcreateCommand:ICommand { Reciever iReciever; Message iMessage; Action iAction; public ConcreateCommand(Action aAction,Message aMesage) { iReciever = new Reciever(); iAction = aAction; iMessage = aMesage; } public void Excute() { iReciever.Operation(iAction,iMessage); } public void UnExcute() { iAction = GetUnDo(); iReciever.Operation(iAction, iMessage); } Action GetUnDo() { switch (iAction) { case Action.Delete: return Action.Insert; case Action.Insert: return Action.Delete; default: return 0; } } } class Invoker { int current; List<ICommand> commandList = new List<ICommand>(); public void Do(ICommand aCommand) { aCommand.Excute(); commandList.Add(aCommand); current++; } public void UnDo(int index) { for (int i = 0; i < index; i++) { commandList[--current].UnExcute(); commandList.RemoveAt(current); } } } class Reciever { public void Operation(Action aAction,Message aMessage) { switch (aAction) { case Action.Delete: Delete(aAction, aMessage); break; case Action.Insert: Insert(aAction, aMessage); break; default: break; } } void Delete(Action aAction,Message aMessage) { Console.WriteLine("删除这些东西"+aAction.ToString()+","+aMessage.Id+","+aMessage.Name); } void Insert(Action aAction, Message aMessage) { Console.WriteLine("增加这些东西" + aAction.ToString() + "," + aMessage.Id + "," + aMessage.Name); } } class Program { static void Main(string[] args) { Message aMessage = new Message(1, "wang"); Message bMessage = new Message(2, "shuai"); Message cMessage = new Message(3, "ni"); Message dMessage = new Message(4, "hao"); ICommand aCommand = new ConcreateCommand(Action.Delete, aMessage); ICommand bCommand = new ConcreateCommand(Action.Insert, bMessage); ICommand cCommand = new ConcreateCommand(Action.Delete, cMessage); ICommand dCommand = new ConcreateCommand(Action.Insert, dMessage); Invoker aInvoker = new Invoker(); aInvoker.Do(aCommand); aInvoker.Do(bCommand); aInvoker.Do(cCommand); aInvoker.Do(dCommand); aInvoker.UnDo(1); aInvoker.UnDo(1); } } }
使用命令模式的优点和缺点:
命令允许请求的一方和接收请求的一方能够独立演化,从而且有以下的优点:
- 命令模式使新的命令很容易地被加入到系统里。
- 允许接收请求的一方决定是否要否决(Veto)请求。
- 能较容易地设计-个命令队列。
- 可以容易地实现对请求的Undo和Redo。
- 在需要的情况下,可以较容易地将命令记入日志。
- 命令模式把请求一个操作的对象与知道怎么执行一个操作的对象分割开。
- 命令类与其他任何别的类一样,可以修改和推广。
- 你可以把命令对象聚合在一起,合成为合成命令。比如宏命令便是合成命令的例子。合成命令是合成模式的应用。
- 由于加进新的具体命令类不影响其他的类,因此增加新的具体命令类很容易。
命令模式的缺点如下:
- 使用命令模式会导致某些系统有过多的具体命令类。某些系统可能需要几十个,几百个甚至几千个具体命令类,这会使命令模式在这样的系统里变得不实际。