编程语言
首页 > 编程语言> > java-命令模式中Invoker类的角色

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

作者:互联网

假设我们在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