java Bill Pugh Singleton为什么使用容器?
我有以下代码:
package DesignPatterns;
public class SingletonProblem
{
public static void main(String[] args) {
System.out.println(BillPughSingleton.getInstance());
System.out.println(BillPughSingletonWithoutContainer.getInstance());
}
}
class BillPughSingletonWithoutContainer
{
private static BillPughSingletonWithoutContainer instance = new BillPughSingletonWithoutContainer();
static {
System.out.println("Static is now loaded in BillPughSingletonWithoutContainer");
}
private BillPughSingletonWithoutContainer() {}
public static BillPughSingletonWithoutContainer getInstance()
{
return instance;
}
}
class BillPughSingleton
{
private static class Container
{
public static BillPughSingleton instance = new BillPughSingleton();
}
private BillPughSingleton() {}
public static BillPughSingleton getInstance()
{
return Container.instance;
}
}
输出为:
DesignPatterns.BillPughSingleton@36baf30c
Static is now loaded in BillPughSingletonWithoutContainer
DesignPatterns.BillPughSingletonWithoutContainer@5ca881b5
如果在没有容器的示例中,在调用BillPughSingletonWithoutContainer.getInstance()
时实例似乎也是延迟加载的,那么容器为什么有用呢
在急切地加载(BillpughSingleton Without容器被称为什么)中,我期望输出是:
Static is now loaded in BillPughSingletonWithoutContainer
DesignPatterns.BillPughSingleton@36baf30c
DesignPatterns.BillPughSingletonWithoutContainer@5ca881b5
而不是:
DesignPatterns.BillPughSingleton@36baf30c
Static is now loaded in BillPughSingletonWithoutContainer
DesignPatterns.BillPughSingletonWithoutContainer@5ca881b5
换句话说,使用容器的好处是什么(在Java中)
# 1 楼答案
这组类显示了定义单例的三种常见方法—两种方法与您定义的方法类似,另一种方法使用
enum
。每个帮助中的System.out.println()
都可以发现初始化中的差异:BillPughSingleton
的主要好处是,实例化只在调用getInstance()
时发生,而对于其他方法,第一个方法访问hello()
会导致实例初始化。enum
版本是一个更简单的声明,可以确保只有一个实例(其他实例需要私有构造函数,在我的示例中省略)这会打印类似这样的内容,因此容器版本的构造将推迟到其
getInstance
调用:# 2 楼答案
在
BillPughSingletonWithoutContainer
中,static {}
初始化器块总是在类第一次被引用时执行(这基本上来自代码的imports
)。这里不存在惰性初始化技巧。根据docs,正确使用静态初始化器是。。。初始化静态字段这与静态内部类不同,后者仅在第一次引用类时加载-这仅发生在
BillPughSingleton
的构造函数中(当它被调用时,而不是在加载时)https://en.wikipedia.org/wiki/Initialization-on-demand_holder_idiom