懒汉式--单例设计模式



/**
 * 懒汉式--单例设计模式
 */
public class LazyUnSafeSingleton {
    //私有化构造方法
    private LazyUnSafeSingleton(){}
    //定义引用
    private static LazyUnSafeSingleton instance;
    //获取对象方法
    public static LazyUnSafeSingleton getInstance(){
        if(instance == null){       //多个线程同时进入到这,会实例多个对象
            instance = new LazyUnSafeSingleton();
        }
        return instance;
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_40333020/article/details/80631924