System.out.println是否违反了得墨忒耳定律?
System.out.println
是否违反了得墨忒耳定律?
如果没有,为什么?
Does System.out.println
violate the law of demeter?
If not, why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
取决于视图。
LoD:是的,因为它使用控制台。在 LoD 下,您无法承担访问权限。
LoD-F:是的,因为它使用了多个点。 LoD-F 规定,在任何方法使用中,只有对象可以知道其自身的内部结构。
IE
需要了解系统的结构(它有 .out)才能到达 println(),
为了系统不破坏 LoD-F,它必须
以 println() (方法)为例来打破正式规则只能访问:
(我知道,这里是反向引用,如代码应该是调用它的方法,但它实际上是双向的。)
Depending on view.
LoD: Yes, because it uses the console. Under LoD you can't assume access.
LoD-F: Yes, because it uses more than one dot. The LoD-F states that in any method usage only the object may know the internal structure of itself.
IE
requires knowledge of the structure of system (that it has .out) to reach println(),
For System to not break LoD-F it would have to be
To break the formal rules down with example, println() (the method) may only access:
(I know, it's a reversed reference here as the code should be the method calling it, but it actually swings both ways.)
System.out
实际上是一个“全局状态”,是的,从技术上讲它违反了“德米特定律”。但是:System.out
is actually a "global state", and yes, technically it violates the "law of demeter". But:System.out.println(..)
. Use a logger (log4j, logback, slf4j) instead.不。
System.out
是一个全局变量。-- Wikipedia
根据第五条规则,你可以调用全局变量
System.out 从任何上下文中。
No.
System.out
is a global variable.-- Wikipedia
By the 5th rule, you can invoke any method of the global variable
System.out
from within any context.我想说不是真的,因为它调用
Object.toString()
并且不以任何方式与任何类紧密耦合;它只是告诉对象将自身转换为字符串。编辑:
如果您指的是调用本身,而不是调用中发生的情况,那么我会说是的,因为它将您的程序与 System.out 字段紧密耦合。
I'd say not really, since it calls
Object.toString()
and doesn't tightly couple with any class in any way; it just tells the object to convert itself to a string.Edit:
If you mean the call itself, rather than what happens in the call, then I'd say yes it does, because it tightly couples your program to the
System.out
field.它违反了法律,因为它使用了“多个点”,并暴露了它正在使用另一个对象进行 println 调用的事实。
It breaks the law, since it uses "more than one dot", and exposes the fact that it is using another object for the println call.