索引
别名
- Action
- Transaction
意图
将一个请求封装为一个对象,从而使你可用不同的请求对客户进行参数化;对请求排队或记录请求日志,以及支持可撤销的操作。
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
结构
参与者
Command
- 声明 Execute 操作的接口。
ConcreteCommand
- 将一个接收者对象绑定于一个动作。
- 调用接收者相应的操作,以实现 Execute。
Client
- 创建一个具体 Command 对象并设定它的接收者。
Invoker
- 要求 Command 执行请求。
Receiver
- 知道如何实施与执行一个请求相关的操作。任何类都可能作为一个接收者。
适用性
在以下情况下可以使用 Command 模式:
- Command 模式是回调(callback)机制的一个面向对象的替代品。所谓回调函数是指函数先在某处注册,而它将在稍后某个需要的时候被调用。
- 在不同的时刻指定、排列和执行请求。Command 对象可以有一个与初始请求无关的生存期。
- 支持取消操作。需要定义 Unexecute 操作来取消 Execute 操作调用的效果。
- 支持修改日志,这样当系统崩溃时,这些修改可以被重做一遍。
- 用构建在原语操作上的高层操作构造一个系统。例如构建事务(Transaction)系统。
效果
- Command 模式将调用操作的对象与知道如何实现该操作的对象解耦。
- Command 是 first-class 对象。它们可像其他的对象一样被操纵和扩展。
- 可以将多个 Command 装配成一个复合 Command。
- 增加新的 Command 很容易,因为无需改变已有的类
相关模式
- Composite 模式可被用来实现 MacroCommand。
- Memento 模式可用来保持某个状态,Command 用这一状态来取消它的效果。
- 可以使用 Prototype 来拷贝 Command 对象。
实现
实现方式(一):直接注入 Receiver 对象,Command 决定调用哪个方法。
namespace CommandPattern.Implementation1
{
public abstract class Command
{
public abstract void Execute();
} public class ConcreteCommand : Command
{
private Receiver _receiver; public ConcreteCommand(Receiver receiver)
{
_receiver = receiver;
} public override void Execute()
{
_receiver.Action();
}
} public class Receiver
{
public void Action()
{
// do something
}
} public class Invoker
{
private Command _cmd; public void StoreCommand(Command cmd)
{
_cmd = cmd;
} public void Invoke()
{
if (_cmd != null)
{
_cmd.Execute();
}
}
} public class Client
{
public void TestCase1()
{
Receiver receiver = new Receiver();
Command cmd = new ConcreteCommand(receiver); Invoker invoker = new Invoker();
invoker.StoreCommand(cmd); invoker.Invoke();
}
}
}
实现方式(二):注入 Receiver 的指定方法,Command 仅能调用该方法。
namespace CommandPattern.Implementation2
{
public abstract class Command
{
public abstract void Execute();
} public class ConcreteCommand : Command
{
private Action _action; public ConcreteCommand(Action action)
{
_action = action;
} public override void Execute()
{
_action.Invoke();
}
} public class Receiver
{
public void Action()
{
// do something
}
} public class Invoker
{
private Command _cmd; public void StoreCommand(Command cmd)
{
_cmd = cmd;
} public void Invoke()
{
if (_cmd != null)
{
_cmd.Execute();
}
}
} public class Client
{
public void TestCase2()
{
Receiver receiver = new Receiver();
Command cmd = new ConcreteCommand(receiver.Action); Invoker invoker = new Invoker();
invoker.StoreCommand(cmd); invoker.Invoke();
}
}
}
实现方式(三):参数化 Command 构造。
namespace CommandPattern.Implementation3
{
public abstract class Command
{
public abstract void Execute();
} public class ConcreteCommand : Command
{
private Action<string> _action;
private string _state; public ConcreteCommand(Action<string> action, string state)
{
_action = action;
_state = state;
} public override void Execute()
{
_action.Invoke(_state);
}
} public class Receiver
{
public void Action(string state)
{
// do something
}
} public class Invoker
{
private Command _cmd; public void StoreCommand(Command cmd)
{
_cmd = cmd;
} public void Invoke()
{
if (_cmd != null)
{
_cmd.Execute();
}
}
} public class Client
{
public void TestCase3()
{
Receiver receiver = new Receiver();
Command cmd = new ConcreteCommand(receiver.Action, "Hello World"); Invoker invoker = new Invoker();
invoker.StoreCommand(cmd); invoker.Invoke();
}
}
}
实现方式(四):使用泛型减少 Command 子类。
namespace CommandPattern.Implementation4
{
public abstract class Command
{
public abstract void Execute();
} public class ConcreteCommand<T, S> : Command
{
private Action<T, S> _action;
private T _state1;
private S _state2; public ConcreteCommand(Action<T, S> action, T state1, S state2)
{
_action = action;
_state1 = state1;
_state2 = state2;
} public override void Execute()
{
_action.Invoke(_state1, _state2);
}
} public class Receiver
{
public void Action(string state1, int state2)
{
// do something
}
} public class Invoker
{
private Command _cmd; public void StoreCommand(Command cmd)
{
_cmd = cmd;
} public void Invoke()
{
if (_cmd != null)
{
_cmd.Execute();
}
}
} public class Client
{
public void TestCase4()
{
Receiver receiver = new Receiver();
Command cmd = new ConcreteCommand<string, int>(
receiver.Action, "Hello World", ); Invoker invoker = new Invoker();
invoker.StoreCommand(cmd); invoker.Invoke();
}
}
}
实现方式(五):使用弱引用代替对 Receiver 的强引用。
namespace CommandPattern.Implementation5
{
public class WeakAction
{
public WeakAction(Action action)
{
Method = action.Method;
Reference = new WeakReference(action.Target);
} protected MethodInfo Method { get; private set; }
protected WeakReference Reference { get; private set; } public bool IsAlive
{
get { return Reference.IsAlive; }
} public object Target
{
get { return Reference.Target; }
} public void Invoke()
{
if (Method != null && IsAlive)
{
Method.Invoke(Target, null);
}
}
} public abstract class Command
{
public abstract void Execute();
} public class ConcreteCommand : Command
{
private WeakAction _action; public ConcreteCommand(Action action)
{
_action = new WeakAction(action);
} public override void Execute()
{
_action.Invoke();
}
} public class Receiver
{
public void Action()
{
// do something
}
} public class Invoker
{
private Command _cmd; public void StoreCommand(Command cmd)
{
_cmd = cmd;
} public void Invoke()
{
if (_cmd != null)
{
_cmd.Execute();
}
}
} public class Client
{
public void TestCase5()
{
Receiver receiver = new Receiver();
Command cmd = new ConcreteCommand(receiver.Action); Invoker invoker = new Invoker();
invoker.StoreCommand(cmd); invoker.Invoke();
}
}
}
实现方式(六):使 Command 支持 Undo 和 Redo。
如果 Command 提供方法逆转操作,例如 Undo 操作,就可以取消执行的效果。为达到这个目的,ConcreteCommand 类可能需要存储额外的状态信息。
这个状态包括:
接收者对象,它真正执行处理该请求的各操作。
接收者上执行操作的参数。
如果处理请求的操作会改变接收者对象中的某些值,那么这些值也必须先存储起来。接收者还必须提供一些操作,以使该命令可将接收者恢复到它先前的状态。
namespace CommandPattern.Implementation6
{
public abstract class Command
{
public abstract void Execute();
public abstract void Unexecute();
public abstract void Reexecute();
} public class ConcreteCommand : Command
{
private Receiver _receiver;
private string _state;
private string _lastState; public ConcreteCommand(Receiver receiver, string state)
{
_receiver = receiver;
_state = state;
} public override void Execute()
{
_lastState = _receiver.Name;
_receiver.ChangeName(_state);
} public override void Unexecute()
{
_receiver.ChangeName(_lastState);
_lastState = string.Empty;
} public override void Reexecute()
{
Unexecute();
Execute();
}
} public class Receiver
{
public string Name { get; private set; } public void ChangeName(string name)
{
// do something
Name = name;
}
} public class Invoker
{
private Command _cmd; public void StoreCommand(Command cmd)
{
_cmd = cmd;
} public void Invoke()
{
if (_cmd != null)
{
_cmd.Execute();
}
} public void UndoInvoke()
{
if (_cmd != null)
{
_cmd.Unexecute();
}
}
} public class Client
{
public void TestCase6()
{
Receiver receiver = new Receiver();
Command cmd = new ConcreteCommand(receiver, "Hello World"); Invoker invoker = new Invoker();
invoker.StoreCommand(cmd); invoker.Invoke();
invoker.UndoInvoke();
}
}
}
实现方式(七):使 MacroCommand 来管理 Command 序列。
MacroCommand 需要提供增加和删除子 Command 的操作。
namespace CommandPattern.Implementation7
{
public abstract class Command
{
public abstract void Execute();
} public class MacroCommand : Command
{
private List<Command> _cmdList = new List<Command>(); public MacroCommand()
{
} public void Add(Command cmd)
{
_cmdList.Add(cmd);
} public void Remove(Command cmd)
{
_cmdList.Remove(cmd);
} public override void Execute()
{
foreach (var cmd in _cmdList)
{
cmd.Execute();
}
}
} public class ConcreteCommand1 : Command
{
private Receiver _receiver; public ConcreteCommand1(Receiver receiver)
{
_receiver = receiver;
} public override void Execute()
{
_receiver.Action1();
}
} public class ConcreteCommand2 : Command
{
private Receiver _receiver; public ConcreteCommand2(Receiver receiver)
{
_receiver = receiver;
} public override void Execute()
{
_receiver.Action2();
}
} public class Receiver
{
public void Action1()
{
// do something
} public void Action2()
{
// do something
}
} public class Invoker
{
private Command _cmd; public void StoreCommand(Command cmd)
{
_cmd = cmd;
} public void Invoke()
{
if (_cmd != null)
{
_cmd.Execute();
}
}
} public class Client
{
public void TestCase7()
{
Receiver receiver = new Receiver();
Command cmd1 = new ConcreteCommand1(receiver);
Command cmd2 = new ConcreteCommand2(receiver);
MacroCommand macro = new MacroCommand();
macro.Add(cmd1);
macro.Add(cmd2); Invoker invoker = new Invoker();
invoker.StoreCommand(macro); invoker.Invoke();
}
}
}
《设计模式之美》为 Dennis Gao 发布于博客园的系列文章,任何未经作者本人同意的人为或爬虫转载均为耍流氓。