被动观点是否违反了迪米特法则?
我试图了解如何正确使用被动视图。 在我看来,我在被动视图上看到的每个例子都违反了德米特定律:
//In the presenter code
myview.mytextfield.text = "whatever";
那么被动视图的更好实现是什么?
I'm trying to understand how to use Passive View correctly. It seems to me that every examples I look at on Passive View breaks the Law of Demeter :
//In the presenter code
myview.mytextfield.text = "whatever";
So what's a better implementation of Passive View?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
首先,与大多数编程规则一样,德墨忒尔定律更多的是一种原则或指导方针,有时该原则并不适用。 话虽这么说,德米特定律并不真正适用于被动视图,因为在这种情况下,该定律的原因不是问题。
德米特法则试图阻止依赖链,例如:
显然,如果 objectB 的实现更改为使用 objectD,那么它将破坏 objectA 的依赖关系并导致编译错误。 如果采取极端的做法,每当链条因实施变更而受到干扰时,您都会进行猎枪手术。
在被动视图的情况下,
因此,您给出的示例通常会被实现:
而视图将类似于:
希望这可以澄清一些事情。
First, the Law of Demeter, like most rules of programming, is more of a principle or guideline and there are occasions when the principle doesn't apply. That being said, the Law of Demeter doesn't really apply to Passive View because the reason for the law isn't a problem in this case.
The Law of Demeter is trying to prevent dependency chaining such as:
Obviously if objectB's implementation changes to use objectD instead then it will break objectA's dependency and cause a compilation error. If taken to an extreme you wind up doing shotgun surgery any time the chain is disturbed by an implementation change.
In the case of Passive View
So the example you gave would normally be implemented:
While the view would be something like:
Hope this clarifies things a bit.
更好的实现是在 Presenter 和 View 之间使用 API。 Presenter 将通过单个方法(在 View 的接口中定义)将数据推送到其 View。 视图将根据某些内部逻辑管理新输入。
因此,演示者不必了解有关视图的任何信息,并且德米特法则是安全的。
A better implementation would be to have an API between the Presenter and the View. The Presenter would push data to its View through a single method (defined in the View's interface). The View would manage the new input according to some internal logic.
Therefore, the Presenter does not have to know anything about the View and the Law of Demeter is safe.
好吧,是的,这确实违反了德米特定律,该定律基本上说对象的接口不应该揭示对象的实现。 但是,第二个也为实现提供了很多提示。
我认为是时候问问你总体上是否有正确的界面了。 这些文本字段是什么?? 谁将它们设置在视图中? 视图不应该向模型请求数据,而不是相反?
也许您需要观察者模式——模型保留感兴趣方的列表,并在其内部状态发生变化时通知他们。
啊,那个被动观点。 好久没看过这个了。 基本上,我看到两个部分:其中之一是通过使控制器(而不是模型)驱动所有更新,为了(我认为)效率,他公开了特定的字段方法来更新这些字段。 这确实违反了德墨忒尔定律,毕竟它只是某种隐喻意义上的“定律”,就像墨菲定律一样。 不过,这通常是个好主意。 在这种情况下,我将重做视图并将其用作外观来包装对各个字段的更新。
不过,您不需要观察者模式,因为现在您已经让控制器进行了所有更新。 它增加了整体代码的复杂性和易出错性,因为现在您必须编写控制器来进行并行更新。
Okay, well, yes, that does break the Law of Demeter, which basically says that the interface to an object shouldn't reveal the implementation of the object. But then, the second one gives a helluva lot of hints to the implementation too.
I think it's time to ask if you have the right interface in general. What are these text fields? Who is setting them in the view? Shouldn't the view be asking the Model for data, instead of vice-versa?
Maybe you need the Observer pattern -- the Model keeps a list of interested parties and notifies them when its internal state changes.
Ah, that Passive View. Hadn't looked at that in a long while. Basically, I see two parts: one of them is that by making the Controller (not the model) drive all the updates, for (I presume) efficiency he exposes the specific field methods to update those fields. This does violate the Law of Demeter, which is, after all, only a "law" in some metaphorical sense, like Murphy's Law. It is usually a good idea, though. In this case, I'd redo the View and use it as a facade to wrap the updates to the individual field.
You don't need the Observer pattern though, because now you've got the Controller making all updates. It adds some complexity and error proneness to the overall code, because now you hve to write the Controller to make parallel updates.