Also known as
Action, Transaction
Intent
Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
将请求封装成对象,以便使用不同的请求、队列、或日志将客户端参数化,同时提供可撤销功能。
Explanation
In plain words Allows you to encapsulate actions in objects. The key idea behind this pattern is to provide the means to decouple client from receiver.
命令模式可以将“动作的请求者”从“动作的执行者”对象中解耦; 被解耦的二者之间通过命令对象进行沟通。
Wikipedia says
In object-oriented programming, the command pattern is a behavioral design pattern in which an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. This information includes the method name, the object that owns the method and values for the method parameters.
Programmatic Example
这个例子中有法师、妖精、咒语三个类,法师管理咒语的发放、撤销、并能保存施咒记录,妖精则为被咒语作用的对象。
/**
由咒语组成的类, 此处咒语即命令
/**
决定咒语的投放、撤销等操作的法师,即命令的调用者,管理命令的类
/**
And then it can be used as
Wizard
运行结果为:
Class diagram
Applicability
Use the Command pattern when you want to
- parameterize objects by an action to perfom. You can express such parameterization in a procedural language with a callback function, that is, a function that's registerd somewhere to be called at a later point. Commands are an object-oriented raplacement for callbacks.
- specify, queue, and execute requests at different times. A Command object can have a lifetime independent of the original request. If the receiver of a request can be represented in an address space-independent way, then you can transfer a command object for the request to a different process and fulfill the request there.
- support undo. The Command's execute operation can store state for reversing its effects in the command itself. The Command interface must have an added Unexecute operation that reverses the effects of a previous call to execute. Executed commands are stored in a history list. Unlimited-level undo and redo is achieved by traversing this list backwards and forwards calling unexecute and execute, respectively
- support logging changes so that they can be reapplied in case of a system crash. By augmenting the Command interface with load and store operations, you can keep a persistent log of changes. Recovering from a crash involves reloading logged commands from disk and re-executing them with the execute operation
- structure a system around high-level operations build on primitive operations. Such a structure is common in information systems that support transactions. A transaction encapsulates a set of changes to data. The Command pattern offers a way to model transactions. Commands have a common interface, letting you invoke all transactions the same way. The pattern also makes it easy to extend the system with new transactions
- 命令模式将请求发送者和请求处理者分离开,从而降低了这两个类之间的藕合
- 通过在请求发送者和请求处理者之间增加转发类的方式,从而客户端发出的请求可以在被 处理之前都存放在Invoker(此例中为 Wizard)类的容器中,请求在被执行前就有了一个缓冲。
- 增加新的命令时只需增加新的命令子类即可。
Typical Use Case
- to keep a history of requests
- implement callback functionality
- implement the undo functionality
Real world example
- java.lang.Runnable
- javax.swing.Action
Credits
- Design Patterns: Elements of Reusable Object-Oriented Software