單例模式

          @import url(http://www.aygfsteel.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); 飽漢模式:

          package com.jerry.design.singleton;


          public class FullSingle {

          private static FullSingle instance = null;

          private FullSingle(){

          }


          public synchronized static FullSingle getIntance(){

          if(instance==null){

          instance = new FullSingle();

          }

          return instance

          }

          }


          餓漢模式:


          package com.jerry.design.singleton;


          public class HungrySingle {

          private static HungrySingle instance = new HungrySingle();

          private HungrySingle(){

          }


          public static HungrySingle getIntance(){

          return instance

          }

          }


          測試代碼:

          package com.jerry.design.singleton;



          public class Test {

          public static void main(String[] args) {

          for(int i=0;i<=10;i++){

          System.out.println(FullSingle.getIntance());

          System.out.println(HungrySingle.getIntance());

          }

          // com.jerry.design.singleton.FullSingle@3487a5cc

          // com.jerry.design.singleton.HungrySingle@eb42cbf

          // com.jerry.design.singleton.FullSingle@3487a5cc

          // com.jerry.design.singleton.HungrySingle@eb42cbf

          // com.jerry.design.singleton.FullSingle@3487a5cc

          // com.jerry.design.singleton.HungrySingle@eb42cbf

          // com.jerry.design.singleton.FullSingle@3487a5cc

          // com.jerry.design.singleton.HungrySingle@eb42cbf

          // com.jerry.design.singleton.FullSingle@3487a5cc

          // com.jerry.design.singleton.HungrySingle@eb42cbf

          // com.jerry.design.singleton.FullSingle@3487a5cc

          // com.jerry.design.singleton.HungrySingle@eb42cbf

          // com.jerry.design.singleton.FullSingle@3487a5cc

          // com.jerry.design.singleton.HungrySingle@eb42cbf

          // com.jerry.design.singleton.FullSingle@3487a5cc

          // com.jerry.design.singleton.HungrySingle@eb42cbf

          // com.jerry.design.singleton.FullSingle@3487a5cc

          // com.jerry.design.singleton.HungrySingle@eb42cbf

          // com.jerry.design.singleton.FullSingle@3487a5cc

          // com.jerry.design.singleton.HungrySingle@eb42cbf

          // com.jerry.design.singleton.FullSingle@3487a5cc

          // com.jerry.design.singleton.HungrySingle@eb42cbf


          }


          }



          總結: 保持單jvm下的唯一實例。

           

          posted on 2012-02-19 20:55 瘋狂的蝸牛 閱讀(76) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導航:
           
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統(tǒng)計

          公告

          @import url(http://www.aygfsteel.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css);

          常用鏈接

          留言簿

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          主站蜘蛛池模板: 临汾市| 渭南市| 陈巴尔虎旗| 额尔古纳市| 菏泽市| 河北区| 莲花县| 静乐县| 刚察县| 安平县| 南昌市| 云南省| 泾源县| 广河县| 古浪县| 科技| 阿拉善右旗| 双流县| 兴安县| 南宁市| 罗山县| 金华市| 隆尧县| 日土县| 武汉市| 泗洪县| 航空| 永昌县| 泗水县| 黄平县| 肇东市| 武胜县| 文水县| 江北区| 蓬莱市| 安仁县| 南溪县| 襄汾县| 突泉县| 将乐县| 莱芜市|