简体   繁体   English

为什么java.awt.Dimension有公共变量?

[英]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个公共变量的widthheight

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. 我们只使用getput方法,正确地关注哈希表状态。 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.

相关问题 为什么java.awt.Dimension使用双倍的高度和宽度? - Why does java.awt.Dimension use doubles for height and width? java.awt.Dimension类的使用 - Use of the java.awt.Dimension class 找不到与以下对象匹配的构造函数:java.util.ArrayList(java.awt.Dimension) - Could not find matching constructor for: java.util.ArrayList(java.awt.Dimension) 如何使用poi ppt中java.awt.Dimension中的getImageDimensionInPixels()获取图像大小? - How to get image size using getImageDimensionInPixels() from java.awt.Dimension in poi ppt? 为什么java.awt.Color的每个Color都有两个变量? - Why does java.awt.Color have two variables for every Color? 使用java.awt。*创建事件监听器,为什么它会迫使您使用公共抽象作为类? - Using java.awt.* to create an eventlistener, why does it force you to use public abstract as the class? 为什么Java 8中主要托管接口的接口不必公开? - Why does an interface hosting main in Java 8 not have to be public? 为什么java.lang.reflect.Modifier具有公共构造函数? - Why does java.lang.reflect.Modifier have a public constructor? 为什么 Boolean 对象在 Java 中有一个公共构造函数? - Why does the Boolean object have a public constructor in Java? 为什么可以公开访问Java AWT“ Point”类的“ x”和“ y”变量? - Why are the 'x' and 'y' variables of the Java AWT 'Point' class publicly accessible?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM