適配器模式(一)

          單項適配器模式:
          原接口:

          package com.jerry.design.adapter1.imp;


          public interface InterfaceA {

          public void testA();


          }


          接口實現類:

          package com.jerry.design.adapter1.impl;


          import com.jerry.design.adapter1.imp.InterfaceA;


          public class ImplA implements InterfaceA{


          @Override

          public void testA() {

          System.out.println(" i am do something as InterfaceA!");

          }


          }


          目標接口:

          package com.jerry.design.adapter1.imp;


          public interface InterfaceB {

          public void testB();


          }


          目標接口實現類:


          package com.jerry.design.adapter1.impl;


          import com.jerry.design.adapter1.imp.InterfaceA;

          import com.jerry.design.adapter1.imp.InterfaceB;


          public class ImplB implements InterfaceB{


          private InterfaceA implA;

          public ImplB(InterfaceA implA){

          this.implA = implA;

          }

          @Override

          public void testB() {

          implA.testA();

          }


          }


          測試方法:

          package com.jerry.design.adapter1.client;


          import com.jerry.design.adapter1.imp.InterfaceA;

          import com.jerry.design.adapter1.imp.InterfaceB;

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

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


          public class Test {

          public static void main(String[] args) {

          InterfaceA implA = (InterfaceA) new ImplA();

          InterfaceB implB = (InterfaceB) new ImplB(implA);

          implB.testB();// i am do something as InterfaceA!

          }


          }


          總結:原接口轉換為目標接口


          posted on 2012-02-15 21:26 瘋狂的蝸牛 閱讀(55) 評論(0)  編輯  收藏


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


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          <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);

          常用鏈接

          留言簿

          隨筆檔案

          文章分類

          文章檔案

          搜索

          最新評論

          主站蜘蛛池模板: 水富县| 邵阳县| 黄浦区| 稻城县| 鹰潭市| 财经| 黄陵县| 满洲里市| 泗阳县| 宿迁市| 扬州市| 宣武区| 通辽市| 宝坻区| 东明县| 平远县| 仁寿县| 昌邑市| 定襄县| 类乌齐县| 拜城县| 海阳市| 关岭| 永丰县| 佛学| 周宁县| 万盛区| 闵行区| 武冈市| 斗六市| 长垣县| 小金县| 洛川县| 平山县| 朝阳市| 通州市| 乌兰察布市| 年辖:市辖区| 鄂伦春自治旗| 尉氏县| 仪征市|