Fork me on GitHub

java简单单例设计模式-饿汉式||无线程锁懒汉

 

/***

*饿汉式|

/********

class Singleton{
    public static Singleton s = new Singleton();
    private Singleton(){}
    public static Singleton getInstance(){
      return s;
   }

}

/***

*懒汉式|

/********

class Singleton2{

    private static  Singleton2 s;
    private Singleton2(){}
    public static  Singleton2 getInstance(){
       if( s == null){
            s = new Singleton2();
        }
        return s;
    }
}

 

posted @ 2020-03-25 16:54  bigdata云  阅读(110)  评论(0)    收藏  举报