单例模式
手写单例模式
代码实现
package 剑指offer;
/**
* @author WangXiaoeZhe
* @Date: Created in 2019/11/22 16:01
* @description:
*/
public class Singleton {
/**
* 饿汉式
*/
private static Singleton instance = new Singleton();
private Singleton(){}
static Singleton getInstance() {
return instance;
}
/**
* 懒汉式
*/
private static Singleton instance=null;
private Singleton() {
}
static Singleton getInstance(){
if (instance == null) {
instance=new Singleton();
}
return instance;
}
}