设计模式有个特点,就是一看模式的命名,就大致知道是干什么的,使用场景也可能会和名称上的动作相关。
命令行模式 Command
命令模式把一个请求或者操作封装到一个对象中。命令模式把发出命令的责任和执行命令的责任分割开,委派给不同的对象。命令模式允许请求的一方和发送的一方独立开来,使得请求的一方不必知道接收请求的一方的接口,更不必知道请求是怎么被接收,以及操作是否执行,何时被执行以及是怎么被执行的。系统支持命令的撤消。
问题:
Need to issue requests to objects without knowing anything about the operation being requested or the receiver of the request.
意图:
- Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations.
- Promote “invocation of a method on an object” to full object status
- An object-oriented callback