以下类定义是Singleton模式:
public class Singleton {
private static Singleton instance;
private Singleton() {}
public static Singleton getInstance() {
if (instance == null) {
instance = new Singleton();
}
return instance;
}
}
Run Code Online (Sandbox Code Playgroud)
检索Singleton此类的对象实例的唯一方法是调用该getInstance()方法,该方法确保始终存在该Singleton对象的一个实例.
| 归档时间: |
|
| 查看次数: |
14549 次 |
| 最近记录: |