Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
Four terms always associated with the command pattern are command, receiver, invoker and client. A concrete command object has a receiver object and invokes a method of the receiver in a way that is specific to that receiver's class. The receiver then does the work. A concrete command object is separately passed to an invoker object, which invokes the command, and optionally does bookkeeping about the command execution. Any concrete command object can be passed to the same invoker object. Both an invoker object and several concrete command objects are held by a client object. The client contains the decision making about which commands to execute at which points. To execute a command, it passes the command object to the invoker object.
将方法(请求)定义为对象,这些对象即为命令。支持命令的排队、记录以及undo功能。
代码:TBD
本文介绍命令模式的基本概念,包括如何将请求封装成对象以便参数化客户端、实现请求的队列和记录功能,以及如何支持撤销操作。文章还讨论了命令模式中的四个核心组件:命令、接收者、调用者和客户端。
334

被折叠的 条评论
为什么被折叠?



