簡單工廠

          @import url(http://www.aygfsteel.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @import url(http://www.aygfsteel.com/CuteSoft_Client/CuteEditor/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/css/cuteeditor.css); @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.simpleFactory.imp;


          /**

           * 

           * @author jerry

           *

           */

          public interface interfaceTest {

          public void test(String name);


          }


          實現類A:

          package com.jerry.design.simpleFactory.impl;


          import com.jerry.design.simpleFactory.imp.interfaceTest;


          /**

           * 

           * @author jerry

           *

           */

          public class ImplA implements interfaceTest {

          public void test(String name){

          System.out.println(" my ImplA name is:"+name);

          }


          }


          實現類B:

          package com.jerry.design.simpleFactory.impl;


          import com.jerry.design.simpleFactory.imp.interfaceTest;


          /**

           * 

           * @author jerry

           *

           */

          public class ImplB implements interfaceTest {

          public void test(String name){

          System.out.println(" my ImplB name is:"+name);

          }


          }


          工廠類:

          package com.jerry.design.simpleFactory.factory;


          import com.jerry.design.simpleFactory.imp.interfaceTest;

          import com.jerry.design.simpleFactory.impl.ImplA;

          import com.jerry.design.simpleFactory.impl.ImplB;


          public class Factory {

          public static interfaceTest createImp(int type){

          interfaceTest test = null;

          if(type==1){

          test  = (interfaceTest) new ImplA();

          }else if(type==2){

          test = (interfaceTest) new ImplB();

          }

          return test;

          }


          }


          測試類:

          package com.jerry.design.simpleFactory.factory;


          import com.jerry.design.simpleFactory.imp.interfaceTest;


          public class Test {


          public static void main(String[] args) {

          interfaceTest test = Factory.createImp(1);

          test.test(" i am jerry!");// my ImplA name is: i am jerry!

          test = Factory.createImp(2);

          test.test(" i am jerry!");// my ImplB name is: i am jerry!

          }

          }



          總結:簡單工廠其實就是要選擇實現的類而已。

           

           

           

           

          posted on 2012-02-14 22:19 瘋狂的蝸牛 閱讀(55) 評論(0)  編輯  收藏 所屬分類: 設計模式


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


          網站導航:
           
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導航

          統計

          公告

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

          常用鏈接

          留言簿

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          主站蜘蛛池模板: 扎赉特旗| 宜阳县| 青河县| 梅州市| 隆安县| 葫芦岛市| 从江县| 焦作市| 浦北县| 西城区| 普宁市| 宁陵县| 新丰县| 湘乡市| 旺苍县| 蒙阴县| 锦州市| 博客| 达日县| 石渠县| 兰西县| 饶阳县| 蓝山县| 万年县| 正镶白旗| 吉木乃县| 安龙县| 宁晋县| 仪陇县| 繁峙县| 深圳市| 搜索| 阳曲县| 信阳市| 花垣县| 贡觉县| 旬阳县| 疏勒县| 晋州市| 斗六市| 花莲县|