何时使用公共领域可以接受? [英] When is using public fields acceptable?

查看:91
本文介绍了何时使用公共领域可以接受?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主类,它有一个线程池,很多其他类使用它来对数据库执行操作。我目前有一个getter方法来获取工作正常但看起来有点笨拙的池。

I have a main class that has a thread pool, which is used by quite a few other classes for performing actions on a database. I currently have a getter method to get the pool which works fine but seems a bit clumsy.

在任何情况下都可以使用公共字段而不是getter / setter方法?

Are there any circumstances where it is acceptable to use a public field instead of getter/setter methods?

推荐答案


在任何情况下都可以使用公共字段代替getter / setter方法?

Are there any circumstances where it is acceptable to use a public field instead of getter/setter methods?

公共字段不好的主要原因是它们将实现暴露给外界。这会导致不必要的耦合;即过分依赖于其他类的实现细节的类。这往往会使代码更难理解,更难以改变。如果该字段不是 final ,则需要搜索整个代码库,以确保确定没有任何干扰该字段。 (好吧,IDE会让这更容易......但是将公共字段与没有setter的私有字段进行对比。)

The main reason that public fields are bad are that they expose the implementation to the outside world. That leads to unwanted coupling; i.e. classes that are overly dependent on the implementation details of other classes. That tends to make code harder to understand and harder to change. And if the field is not final, you need to search the entire code-base to be sure that nothing is "interfering" with the field. (OK, IDE's make this easier ... but contrast a public field with a private field that has no setter.)

次要原因是你不能覆盖字段。一旦在超类中公开了一个字段,你就无法在子类中做任何修改或限制其含义的事情。 (相比之下,getter和setter可以被覆盖......)

A secondary reason is that you cannot override a field. Once you have exposed a field in a superclass, there is nothing you can do in a subclass can do to modify or restrict its meaning. (By contrast, getters and setters can be overridden ...)

唯一可以接受(从文体角度来看)拥有公共字段的情况是声明字段的类是私有嵌套类或内部类。结果是该字段的所有依赖项都限制在包含声明的源文件中......这可以消除上述问题。

The only situation where it is acceptable (from a stylistic perspective) to have "public" fields is when the class which declares the fields is a private nested or inner class. The consequence is that all dependencies on the field are restricted to the source file that contains the declaration ... which negates the problems above.

UPDATE - 我忘记了 public static final ...但我们根本不会将这些视为字段。无论如何,通常的做法是直接访问 public static final 字段。常量的想法是故意公开名称,类型和值......由于静态字段的性质,覆盖问题不适用。

UPDATE - I forgot public static final ... but we tend not to think of those as fields at all. Anyway, it is normal practice to access public static final fields directly. The idea of a constant is to deliberately expose the name, type and value ... and the override issue doesn't apply because of the nature of static fields.


我目前有一个getter方法来获取池工作正常但看起来有点笨拙。

I currently have a getter method to get the pool which works fine but seems a bit clumsy.

笨拙是一个意见/品味的问题。就个人而言,我认为 obj.getInstance() obj.instance 相比是笨拙的。它只是Java方式 1

"Clumsy" is a matter of opinion / taste. Personally, I don't think that obj.getInstance() is clumsy compared with obj.instance. It is just the Java way1.

另一方面,如果你没有 getInstance() 方法,使用该池的所有类都必须具有对实例字段的硬编码引用。如果(由于某些原因)你需要改变访问池的方式(例如添加安全检查,添加计数器,使池创建变得懒惰,确保访问正确同步),那么你必须改变每个和您编码引用该字段的每个地方。但是对于一个吸气剂,你只有一个地方可以改变。

The flipside is that if you didn't have a getInstance() method, all of the classes that used the pool would have to have hard-coded references to the instance field. If (for some reasons) you needed to change something about the way the pool was accessed (e.g. add a security check, add a counter, make pool creation lazy, make sure that access is properly synchronized), then you have to change each and every place where you have coded reference to the field. But with a getter, you just have one place to change.

1 - 显然,其他语言的做法不同。但你不是在写这些语言。 Java就是这样。

这篇关于何时使用公共领域可以接受?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆