簡單工廠

          @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);

          常用鏈接

          留言簿

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          主站蜘蛛池模板: 济宁市| 牡丹江市| 博罗县| 观塘区| 武邑县| 昌图县| 阿拉善左旗| 上饶县| 富锦市| 潮安县| 搜索| 靖江市| 长丰县| 宁都县| 祁阳县| 桂阳县| 韶山市| 福清市| 淄博市| 行唐县| 冀州市| 通州区| 正宁县| 凯里市| 南靖县| 阜宁县| 襄汾县| 盐亭县| 龙江县| 仲巴县| 营口市| 两当县| 全椒县| 德江县| 金坛市| 余江县| 连平县| 云阳县| 洛浦县| 繁峙县| 垣曲县|