單例模式

          @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


          }


          }



          總結(jié): 保持單jvm下的唯一實(shí)例。

           

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


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


          網(wǎng)站導(dǎo)航:
           
          <2025年8月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          導(dǎo)航

          統(tǒng)計(jì)

          公告

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

          常用鏈接

          留言簿

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          主站蜘蛛池模板: 武夷山市| 涞水县| 安仁县| 册亨县| 满洲里市| 石家庄市| 长沙市| 融水| 五峰| 镇平县| 浦县| 德令哈市| 璧山县| 清新县| 南漳县| 夏津县| 方城县| 滨州市| 二连浩特市| 孝昌县| 淮安市| 台湾省| 和田市| 阿合奇县| 额尔古纳市| 西和县| 永丰县| 吴桥县| 金堂县| 邹平县| 黄龙县| 福清市| 大竹县| 贵德县| 烟台市| 台山市| 晋宁县| 施秉县| 西乡县| 湟中县| 玛纳斯县|