閱讀89 返回首頁    go 阿裏雲 go 技術社區[雲棲]


單例設計模式的實現代碼

 

package test;

public class Singleton {

 /**
  *
  * @author Administrator/2012-3-1/上午09:07:06
  */
 public static void main(String[] args) {

  Singleton s1=Singleton.getInstance();
  Singleton s2=Singleton.getInstance();
  Singleton s3=Singleton.getInstance();
  System.out.println("s1="+s1);
  System.out.println("s2="+s2);
  System.out.println("s3="+s3);
  

  

 }

  /*** SingletonHolder is loaded on the first execution of Singleton.getInstance() 
   * * or the first access to SingletonHolder.INSTANCE, not before.  
   * */
 private static class SingletonHolder {
  public static final Singleton INSTANCE = new Singleton();
 }

 //Private constructor prevents instantiation from other classes
 private Singleton() {
 }

 public static Singleton getInstance() {
  return SingletonHolder.INSTANCE;
 }

}

最後更新:2017-04-02 22:16:29

  上一篇:go Hibernate之update(2)——報錯query must begin with SELECT or FROM
  下一篇:go 關於ORACLE時間存儲的問題