[英]Why does java.awt.Dimension have public variables?
Isn't it true that a class with public
variables is considered weak in Encapsulation and is it not a bad design practice? 具有
public
变量的类在Encapsulation中被认为是弱的并且它是不是一个糟糕的设计实践?
If such is the case why does java.awt.Dimension
have 2 public variables width
and height
? 如果是这种情况,为什么
java.awt.Dimension
有2个公共变量的width
和height
?
I think that exposing public fields is not a violation of encapsulation itself. 我认为暴露公共领域并不违反封装本身。 Encapsulation is the property of entities, that hide inside themself a complex inner structure.
封装是实体的属性,隐藏在自身内部是一个复杂的内部结构。 This complex structure can't be accessed directly and therefore not being risked to be broken.
这种复杂的结构不能直接访问,因此不会有被破坏的风险。 It is accessed only through public methods, which make their work well and don't corrupt the complex inner structure.
它只能通过公共方法访问,这使得它们的工作很好并且不会破坏复杂的内部结构。 For example, we don't have access to
HashMap
's hashtable directly and we can't corrupt it. 例如,我们无法直接访问
HashMap
的哈希表,也无法破坏它。 We only use get
and put
methods, that care of hashtable state properly. 我们只使用
get
和put
方法,正确地关注哈希表状态。 But where is the complex structure of Dimension
object? 但是
Dimension
对象的复杂结构在哪里? What things get broken when we just write d.width = 23
? 当我们写
d.width = 23
时会发生什么事情?
I think there are reasons to use public getters/setters instead of public fields in this case: taste and conventions. 在这种情况下,我认为有理由使用公共getter / setter而不是公共字段:品味和惯例。 The latter is more significant, I suppose.
我想,后者更重要。 But it also depends on where and for what we apply this conventions.
但它也取决于我们应用这些约定的地点和内容。
If you look at the JavaDoc for Dimension: http://docs.oracle.com/javase/7/docs/api/java/awt/Dimension.html 如果你看看JavaDoc for Dimension: http : //docs.oracle.com/javase/7/docs/api/java/awt/Dimension.html
You will see that the public fields have been there since 1.0 (1996). 你会看到自1.0(1996)以来公共领域一直存在。 The language has evolved to accomodate many principles and best practices over time to become the language that it is today.
随着时间的推移,语言已经发展到适应许多原则和最佳实践,成为今天的语言。 This included a reworking of the AWT model for the 1.1 release, of which the accessors and mutators where added for these fields.
这包括对1.1版本的AWT模型进行重新处理,其中为这些字段添加了访问器和更改器。
Here are some archived release notes for the JDK 1.1 release: http://www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/guide/awt/HowToUpgrade.html 以下是JDK 1.1版本的一些存档发行说明: http : //www.cis.upenn.edu/~bcpierce/courses/629/jdkdocs/guide/awt/HowToUpgrade.html
From which this is quoted: 从中引用:
"These changes make it possible for programs such as GUI builders and JavaBeans-using programs to query components to determine the components' properties."
“这些更改使GUI构建器和JavaBeans使用程序等程序可以查询组件以确定组件的属性。”
Dimension has been around for a really long time and in the early days of Java there was a real concern about performance. Dimension已经存在很长时间了,在Java的早期阶段,人们对性能产生了真正的担忧。 So they used public variables and made it mutable - two major mistakes, just to gain a few clock cycles.
因此,他们使用公共变量并将其变为可变 - 两个主要错误,只是为了获得几个时钟周期。
All you guys asking questions about premature optimizations can learn a good lesson here. 所有人都在询问有关过早优化的问题,可以在这里学到很好的教训。 (Added)
(添加)
Dimensions change a lot (as a window is resized) and by making it mutable you "save" on having to allocate new objects - just reuse the old one. 维度变化很大(当窗口调整大小时)并且通过使其变得可变,您“保存”必须分配新对象 - 只需重用旧对象。 Which, in the early days of Java, made some sense.
在Java的早期,这是有道理的。 Plus, they got accessed a lot as the GUI drew nested Buttons within Components inside Frames, so, to save a few cycles in a redraw don't use a function call.
另外,随着GUI在Frames中的组件内绘制嵌套按钮,他们被访问了很多,因此,为了在重绘中保存几个周期,不要使用函数调用。 Again, makes a little sense.
再一次,有点意义。
The problem is that nowadays with all the multi-core processors and multi-threaded code you often en up making defensive copies of a Dimension. 问题在于,如今所有的多核处理器和多线程代码都会让你经常制作一个Dimension的防御性副本。 Losing all that "advntage", plus, annoying and a source of bugs.
失去所有“优点”,加上,烦人和错误的来源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.