当客户端可以直接从接收器实例调用方法时,为什么选择命令模式?

发布于 2024-12-21 01:48:40 字数 199 浏览 5 评论 0原文

最近我遇到了命令模式。在此模式中,客户端负责创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋日私语 2024-12-28 01:48:40

当客户端可以时,为什么我需要 Invoker(命令模式)
使用接收器直接调用特定操作?

这是命令模式的主要设计原理之一 - 将调用者与调用者解耦接收者(发送者-接收者/生产者-消费者)。

Why do I need an Invoker (Command Pattern) when the client can
directly call the particular action using Reciever?

This is the one of the main design rationales of the Command Pattern - decouple an invoker from a receiver (sender-receiver / produced-consumer).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文