ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-命令模式中Invoker类的角色

2019-10-27 02:03:09  阅读:177  来源: 互联网

标签:command-pattern java design-patterns


假设我们在this way中实现了命令模式

我对Invoker在这里的角色有些困惑.从我的角度来看:

>如果我们确实需要历史记录(或命令执行前的任何类型的动作),则可以使用此类.但这违反了单一责任原则,是吗?现在,它不仅是一个委托,而且还在那里存储历史记录.
>如果我们不需要历史记录,那么我看不到创建此调用程序的目标,该调用程序仅执行委派.是个
这样做的唯一原因只是假设,将来我们在执行命令之前/之后需要某种逻辑吗?

还是我错过了什么?

解决方法:

If we do need history (or any kind of action before command execution), then there is a sense in making this class. But then it breaks Single responsibility principle, yeah? Now it’s not only a delegate, it also stores history there.

我完全同意安德里亚斯的回答.如果您认为您正在执行多个职责,请将它们分为不同的方法.

“单一责任”原则令人耳目一新,但我们不应对此原则给予过多关注.如果您严格遵循该原则,我相信代码库中杂乱的小类太多了.我认为软件行业的任何大型项目都不会使用该原理.我们可以做的最好的事情是在同一个类中针对不同的动作使用不同的方法.

If we don’t need history, I don’t see a goal of creating this invoker, that simply performs delegating. Is the only reason for it is just a assumption, that we would need some kind of logic before/after command execution in the future?

命令模式的核心USP是调用程序.它使客户端(发送者)和接收者解耦.

oodesign文章:

The Client asks for a command to be executed. The Invoker takes the command, encapsulates it and places it in a queue, in case there is something else to do first, and the ConcreteCommand that is in charge of the requested command, sending its result to the Receiver.

我已经在以下SE问题中解释了Invoker的作用:

Command Pattern seems needlessly complex (what am I failing to understand?)

标签:command-pattern,java,design-patterns
来源: https://codeday.me/bug/20191027/1940967.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有