單例模式

          @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 瘋狂的蝸牛 閱讀(78) 評論(0)  編輯  收藏


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


          網站導航:
           
          <2025年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導航

          統計

          公告

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

          常用鏈接

          留言簿

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          主站蜘蛛池模板: 天长市| 嵩明县| 岳西县| 朔州市| 海丰县| 开封县| 兴化市| 乐都县| 宾阳县| 南开区| 玉门市| 闽侯县| 临夏市| 榆中县| 宁河县| 新龙县| 白沙| 兰溪市| 大埔县| 西充县| 左贡县| 蓬安县| 漳平市| 克东县| 辽宁省| 河津市| 黄石市| 日喀则市| 华容县| 中西区| 于田县| 理塘县| 娱乐| 襄汾县| 陆丰市| 米林县| 元朗区| 滨海县| 海宁市| 张家港市| 邹平县|