command对象提供的3个execute方法是_Command (C design pattern)

2420798bf28bc152de9101c00381c8c8.png

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 

运行结果为:

d38b066af995c05cc8d2d154b797b036.png

Class diagram

7a780a5a510a30ea5a8728203a3598ec.png

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
  1. 命令模式将请求发送者和请求处理者分离开,从而降低了这两个类之间的藕合
  2. 通过在请求发送者和请求处理者之间增加转发类的方式,从而客户端发出的请求可以在被 处理之前都存放在Invoker(此例中为 Wizard)类的容器中,请求在被执行前就有了一个缓冲。
  3. 增加新的命令时只需增加新的命令子类即可。

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值