少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯系 :: 聚合  :: 管理
            500 Posts :: 0 Stories :: 135 Comments :: 0 Trackbacks
          1、餓漢模式:

          package com.abin.info.service;

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

          }


          private static LazyMode lazyMode=null;
          public static synchronized LazyMode getInstance(){
          if(null==lazyMode){
          lazyMode=new LazyMode();
          }
          return lazyMode;


          2、懶漢模式:

          package com.abin.inter.she;

          public class Singleton {
           private static Singleton singleton=null;
           private Singleton(){
            
           }
           public static Singleton getInstance(){
            if(null==singleton){
             return new Singleton();
            }
            return singleton;
           }

          }

          懶漢模式的線程安全版本:

          package com.abin.lee.template.pattern;
          public class LazyMode {
          private static LazyMode lazyMode=null;
          public static synchronized LazyMode getInstance(){
          if(null==lazyMode){
          lazyMode=new LazyMode();
          }
          return lazyMode;
          }



          3、雙重檢測:

           

          package com.east.abin.impl;

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

          }

           







          4.靜態內部類:
          單例模式創新!google的ioc作者寫的。只有在調用的時候才會初始化!而且線程安全   
          超級牛!

          package com.east.abin.bin;

          public class Singleton {
           private Singleton(){
            
           }
           
           private static class SingletonHelp{
            private static Singleton instance=new Singleton();
           }
           
           public static Singleton getInstance(){
            return SingletonHelp.instance;
           }
           

          }




          5、enum類型的(這個是針對jdk 1.5以及1.5版本以上的)

          package com.abin.peng.service;

          public enum Singleton {
           Singleton;
           private Singleton(){}
           public static Singleton getInstance(){
            return Singleton;
           }
          }

           

          posted on 2012-03-15 18:21 abin 閱讀(520) 評論(0)  編輯  收藏 所屬分類: PatternDesigns
          主站蜘蛛池模板: 鄂尔多斯市| 洪雅县| 冕宁县| 娱乐| 盐津县| 北海市| 都安| 布拖县| 文昌市| 临邑县| 项城市| 司法| 布尔津县| 嘉善县| 南雄市| 连州市| 枞阳县| 木兰县| 邯郸市| 墨玉县| 岫岩| 乐昌市| 响水县| 伊通| 郁南县| 额尔古纳市| 台安县| 富源县| 临颍县| 霍林郭勒市| 邢台市| 汤阴县| 闸北区| 万州区| 咸阳市| 星座| 公主岭市| 武穴市| 新干县| 南昌市| 莱阳市|