- 要点
- 实现
- 代码
- 某个类只能有一个实例
- 必须自行创建实例
- 必须自行向整个系统提供这个实例
- 只提供私有的构造方法
- 含有一个该类的静态私有的对象
- 提供静态的公有方法用于创建、获取静态私有对象
饿汉式
public class SingleInstance {
private SingleInstance() {
}
private static SingleInstance instance = new SingleInstance();
public static SingleInstance getInstance() {
return instance;
}
}
懒汉式
public class SingleInstance {
private SingleInstance() {
}
private static SingleInstance instance = null;
public static SingleInstance getInstance() {
if (instance == null)
instance = new SingleInstance();
return instance;
}
}



