首页 > 其他 > 详细

单例模式

时间:2018-12-10 20:36:18      阅读:167      评论:0      收藏:0      [点我收藏+]
//1.懒汉模式

public class SingletonDemo {
    private static SingletonDemo instance;
    private SingletonDemo(){
    }
    public static SingletonDemo getInstance(){
        if(instance==null){
            instance=new SingletonDemo();
        }
        return instance;
    }
}
//2. 线程安全的懒汉模式

public class SingletonDemo {
    private static SingletonDemo instance;
    private SingletonDemo(){
    }
    public static synchronized SingletonDemo getInstance(){
        if(instance==null){
            instance=new SingletonDemo();
        }
        return instance;
    }
}

//3.饿汉

public class SingletonDemo {
    private static SingletonDemo instance=new SingletonDemo();
    private SingletonDemo(){
    }
    public static SingletonDemo getInstance(){
        return instance;
    }
}

 

单例模式

原文:https://www.cnblogs.com/WPF0414/p/10098560.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!