德米特定律非常令人困惑,因为看起来我永远无法编写返回对象的方法
感觉我已经走进了死胡同。如果我理解正确的话,如果我遵循德米特法则,我永远无法创建一个返回的方法一个对象,然后客户端代码调用它。我只是在考虑总是返回一个对象的工厂模式。是的,有返回对象的映射器类。那么收藏又如何呢?
It feels like I've come to a dead end. If I understood it right then if I follow the Law of Demeter I can never make a method that returns an object and then client code makes calls to it. I'm just thinking about the Factory Pattern which always returns an object. Yes, there are mapper classes that return objects. And how about collections?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您误解了德墨忒尔法则,并且对其应用超出了实用范围:
工厂特别用于创建对象,并且它们创建的对象类型是其公共接口的一部分。因此,上面允许调用工厂创建的对象的方法。
You've misunderstood the Law of Demeter and are applying it beyond the point of usefulness:
Factories in particular are used to create an object, and the type of object they create is part of their public interface. Thus, calling methods of an object created by a factory is allowed by the above.