少年阿賓

          那些青春的歲月

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            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.靜態(tài)內(nèi)部類:
          單例模式創(chuàng)新!google的ioc作者寫的。只有在調(diào)用的時候才會初始化!而且線程安全   
          超級牛!

          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 閱讀(515) 評論(0)  編輯  收藏 所屬分類: PatternDesigns
          主站蜘蛛池模板: 桓仁| 专栏| 浪卡子县| 伊通| 普格县| 拉萨市| 恩施市| 建阳市| 巍山| 安福县| 张掖市| 济源市| 扎囊县| 宝清县| 民县| 深水埗区| 宝山区| 余干县| 谢通门县| 红桥区| 曲周县| 寿阳县| 阜平县| 阿巴嘎旗| 望江县| 宁海县| 九台市| 静乐县| 永善县| 平塘县| 遂平县| 建昌县| 南投市| 六枝特区| 鹤峰县| 育儿| 汨罗市| 泰兴市| 黑水县| 高碑店市| 卫辉市|