You want to create a singleton class by using the Singleton design pattern.

Which two statements enforce the singleton nature of the design?

A. Make the class static.
B. Make the constructor private.
C. Override equals() and hashCode() methods of the java.lang.Object class.
D. Use a static reference to point to the single instance.
E. Implement the Serializable interface.

题解

Singleton design pattern 能保证一个类别只能有一个对象实体。

选项 A,静态类别和它能产生多少个实体并没有关联。

选项 B,用 private 来修饰构造器,可以有效地阻绝外部类别任意实体化出对象实体。

选项 C,equals 和 hashCode 方法和对象实体化没有什么关联。

选项 D,用静态的变量来保存第一次实体化出来的对象参考,接下来都返回这个对象参考即可。

选项 E,Serializable 和实体化没有什么关联。