零实例枚举与私有构造函数以防止实例化 [英] Zero instance enum vs private constructors for preventing instantiation

查看:163
本文介绍了零实例枚举与私有构造函数以防止实例化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一些实用程序类(认为 java.lang.Math )只声明一个私有构造函数,以防止类的实例化。



有没有什么特别的理由为什么这样的类不能实现一个0实例枚举?在我看来,像枚举是一种更直接的控制实例化的方式,而不是在构造函数上的访问修饰符。这也阻止了班级本身创造出一个不妨碍程序员在脚下拍摄自己的例子,并向外传递一个保证。



Joshua Bloch主张使用单身单词0-instance实用程序类不应该有同样的好处吗?



我的问题: 0实例枚举的优缺点是什么? vs私有构造函数。 (我个人认为没有使用枚举的缺点,虽然私有构造函数似乎是更流行的方法。)



(我知道 java.lang.Math 早于枚举。我在这里说1.5+代码。)

解决方案

所以,总结答案和意见至今:



参数支持0-实例枚举:




  • 枚举解决了控制实例化的问题,这正是0实例实用程序


  • 平日有7个实例, Month 有12, MySingleton 有1(根据Joshua Bloch的说法,通过一个枚举来实现)和 MyUtilityClass 有0个实例。最后一个案例与前者没有任何概念上的区别。







  • 不遵循最不惊讶的原则;当人们看到一个枚举时,他们期望它遵循非空枚举的例子,如平日,状态代码等。


  • 实例枚举是一种不被广泛使用的成语,因此不是其他程序员容易识别的东西。即枚举使用隐式合成方法,这意味着这些名称不允许用于自定义方法。


  • 此外,公开API公开不应使用的方法的事实可能从尴尬到破裂。




其他注释




Some utility classes (think java.lang.Math) declares only a private constructor in order to prevent instantiation of the class.

Is there any particular reason for why such classes are not implemented in terms of a 0-instance enum? It seems to me like enums is a more direct way of controlling instantiation than access modifiers on constructors. It also prevents the class itself from creating instances which both prevent the programmer from shooting himself in the foot and convey a guarantee outwards of no instances.

Joshua Bloch advocates the use of enums for singletons. Shouldn't the same benefits apply to 0-instance utility classes?

My question: What are the pros/cons of 0-instance enums vs private constructors. (I personally see no drawbacks of using an enum, though private constructors seems to be the more prevalent approach.)

(I know java.lang.Math predates enum. I'm talking 1.5+ code here.)

解决方案

So, to summarize the answers and comments so far:

Arguments supporting 0-instance enums:

  • Enum solves the problem of controlling instantiation of classes which is precisely what a 0-instance utility class needs.

  • Weekday has 7 instances, Month have 12, MySingleton has 1 (and should according to Joshua Bloch be implemented by means of an enum) and MyUtilityClass has 0 instances. There is no conceptual difference between the last case and the former ones.

Arguments against 0-instance enums:

  • Does not follow the principle of least astonishment; when people see an enum, they expect it to follow the text-book examples of non-empty enums such as weekdays, status codes etc.

  • The 0-instance enum is an idiom not widely used and thus not something other programmers recognize easily. I.e. it's less readable than using private constructors.

  • Enums are cluttered with implicit synthetic methods, which means that those names are not allowed for custom-defined methods. Furthermore, the fact that a public API exposes methods which should not be used can range from awkward to broken.

Other notes

这篇关于零实例枚举与私有构造函数以防止实例化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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