命令模式的作用是将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。
由于“行为请求者”与“行为实现者”的紧耦合,使用命令模式,可以对请求排队或记录请求日志,以及支持可撤销的操作。
命令模式把请求一个操作的对象与知道怎么执行一个操作的对象分割开。
Command模式关键就是讲一个请求封装到一个类中(Command),再提供处理对象(Receiver),最后Command命令由Invoker激活。另外,我们可以将请求接收者的处理抽象出来作为参数传给Command对象,实际也就是回调的机制来实现这一点。也就是讲处理操作方法地址通过参数传递给Command对象,Command对象在适当的时候再调用该函数。
Command模式将调用操作的对象和知道如何实现该操作的对象解耦,在上面Command的结构图中,Invoker对象根本就不知道具体的是哪个对象在处理Execute操作(当然要知道是Command类别的对象)。
在Command要增加新的处理操作对象很容易,我们可以通过创建新的继承自Command的子类来实现这一点。
Command模式可以和Memento模式结合起来,支持取消的操作。
结构图:

Command模式通过将请求封装到一个对象Command中,并将请求的接收者存放到具体的ConcreteCommand类中,从而实现调用操作的对象和操作的具体实现者之间的解耦。
Command模式结构图中,将请求的接收者(处理者)放到Command的具体子类ConcreteCommand中,当请求到来时(Invoker发出Invoke消息激活Command对象),ConcreteCommand将处理请求交给Receiver对象进行处理。
例子:
namespace Bridge_DesignPattern
{
using System;
class Abstraction
{
protected Implementation impToUse;
public void SetImplementation(Implementation i)
{
impToUse = i;
}
virtual public void DumpString(string str)
{
impToUse.DoStringOp(str);
}
}
class DerivedAbstraction_One : Abstraction
{
override public void DumpString(string str)
{
str += ".com";
impToUse.DoStringOp(str);
}
}
class Implementation
{
public virtual void DoStringOp(string str)
{
Console.WriteLine("Standard implementation - print string as is");
Console.WriteLine("string = {0}", str);
}
}
class DerivedImplementation_One : Implementation
{
override public void DoStringOp(string str)
{
Console.WriteLine("DerivedImplementation_One - don't print string");
}
}
class DerivedImplementation_Two : Implementation
{
override public void DoStringOp(string str)
{
Console.WriteLine("DerivedImplementation_Two - print string twice");
Console.WriteLine("string = {0}", str);
Console.WriteLine("string = {0}", str);
}
}
/// <summary>
/// Summary description for Client.
/// </summary>
public class Client
{
Abstraction SetupMyParticularAbstraction()
{
// we localize to this method the decision which abstraction and
// which implementation to use. These need to be decided
// somewhere and we do it here. All teh rest of the client
// code can work against the abstraction object.
Abstraction a = new DerivedAbstraction_One();
a.SetImplementation(new DerivedImplementation_Two());
return a;
}
public static int Main(string[] args)
{
Client c = new Client();
Abstraction a = c.SetupMyParticularAbstraction();
// From here on client code thinks it is talking to the
// abstraction, and will not need to be changed as
// derived abstractions are changed.
// more client code using the abstraction goes here
// . . .
a.DumpString("Clipcode");
return 0;
}
}
}
命令模式的优点:
1,它能较容易地设计一个命令队列;
2,在需要的情况下,可以较容易地将命令记入日志;
3,允许接收请求的一方决定是否要否决请求。
4,可以容易地实现对请求的撤销和重做;
5,由于加进新的具体命令类不影响其他的类,因此增加新的具体命令类很容易。
适用场景: