首页 > 编程语言 > 详细

Java设计模式之单例

时间:2017-07-21 19:24:40      阅读:199      评论:0      收藏:0      [点我收藏+]
/**
 * 单例!!!!!!!!!!!!!!!!!!!
 */
public class SingletonTest {
    private static SingletonTest instance = null;
    private SingletonTest(){

    }
    private  static  synchronized  void syncInit(){
        if(instance == null){
            instance = new SingletonTest();
            System.out.println("实例创建成功");
        }
    }
    public static SingletonTest getInstance(){
        if(instance == null){
            syncInit();
        }
        return instance;
    }

}
public class Test {
    public static void main(String[] args){
         SingletonTest singleton =  SingletonTest.getInstance();
         SingletonTest singleton1 = SingletonTest.getInstance();
    }
}

运行结果:

技术分享

即只会创建一次实例!!!!!

 

Java设计模式之单例

原文:http://www.cnblogs.com/alexanderthegreat/p/7219220.html

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