当客户端可以直接从接收器实例调用方法时,为什么选择命令模式?
最近我遇到了命令模式
。在此模式中,客户端负责创建 Reciever、ConcreteCommand 和 Invoker 的实例。在某个时间点(假设在单击按钮时)调用 Invoker 的 Invoke 方法。现在,Invoke 方法负责在 Reciever 中执行特定操作。但是,当客户端可以使用接收器直接调用特定操作时,为什么我需要调用程序(否则命令模式)?
Recently I have come across Command Pattern
. In this pattern Client is responsible for creating the instances of Reciever, ConcreteCommand and Invoker. At some point of time (Lets say that in a Button Click) Invoke method of Invoker is being called. Now the Invoke method is responsible for executing a particular operation in Reciever. But Why do I need an Invoker ( Otherwise Command Pattern) when the client can directly call the particular action using Reciever?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是命令模式的主要设计原理之一 - 将调用者与调用者解耦接收者(发送者-接收者/生产者-消费者)。
This is the one of the main design rationales of the Command Pattern - decouple an invoker from a receiver (sender-receiver / produced-consumer).