適配器(Adaptor)模式.ppt_第1頁
適配器(Adaptor)模式.ppt_第2頁
適配器(Adaptor)模式.ppt_第3頁
適配器(Adaptor)模式.ppt_第4頁
適配器(Adaptor)模式.ppt_第5頁
已閱讀5頁,還剩16頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)

文檔簡介

1、適配器(Adaptor)模式,目的: 把一個已有的類用用戶希望的形式描述;即用用戶希望的接口適配這個類。 在這個模式中,這個類叫做被適配者(Adaptee),用戶定義的接口叫適配接口,用戶實現(xiàn)適配接口的類叫適配器(Adaptor)。也叫做,包裝器(Wrapper)。,Adapter,假設(shè)有一圖形編輯器,允許用戶繪制和排列基本圖元生成復(fù)雜圖形。 該程序中有一個接口:Shape,定義了圖形對象的基本接口服務(wù)。程序中所有圖元對象均實現(xiàn)該接口。 假設(shè)需要設(shè)計一個TextShape類,用來顯示和編輯正文。設(shè)計該類時希望用到現(xiàn)成的TextView類,用來顯示和編輯正文。,Adapter模式,Applica

2、bility:Use the Adapter pattern when you want to use an existing class, and its interface does not match the one you need. 你想使用一個已經(jīng)存在的類,并且它的接口不符合要求。 you want to create a reusable class that cooperates with unrelated or unforeseen classes, that is, classes that dont necessarily have compatible interfa

3、ces. 你想創(chuàng)建一個可以復(fù)用的類,該類可以與其他不相關(guān)的類或不可預(yù)見的類(即那些接口可能不兼容的類)協(xié)同工作。 (object adapter only) you need to use several existing subclasses, but its impractical to adapt their interface by subclassing every one. An object adapter can adapt the interface of its parent class.你想使用一些已經(jīng)存在的子類,但是不可能對每一個都進行子類化以匹配它們的接口。對象適配器

4、可以適配它們的父類接口。,Adapter模式,Struct class adapter object adapter,例子,OozimozRocket,public class OozinozRocket extends PhysicalRocket implements RocketSim private double time; Public OozinozRocket(double burnArea,double burnRate,double fuelMass,double totalMass) super(burnArea,burnRate,fuelMass,totalMass);

5、public double getMass() public double getThrust() public void setSimTime(double time)this.time=time; ,類適配器簡單例子,public interface Print public abstract void printWeak(); public abstract void printStrong(); - public class Banner private String string; public Banner(String string) this.string = string;

6、public void showWithParen() System.out.println( + string + ); public void showWithAster() System.out.println(* + string + *); ,public class PrintBanner extends Banner implements Print public PrintBanner(String string) super(string); public void printWeak() showWithParen(); public void printStrong()

7、showWithAster(); = public class Main public static void main(String args) Print p = new PrintBanner(Hello); p.printWeak(); p.printStrong(); ,對象適配器,Newclass類的實例也是RequiredClass的實例,在其內(nèi)部通過產(chǎn)生的Existingclass的實例來適配usefulMethod方法,對象適配的簡單例子,public abstract class Print public abstract void printWeak(); public

8、abstract void printStrong(); = public class PrintBanner extends Print private Banner banner; public PrintBanner(String string) this.banner = new Banner(string); public void printWeak() banner.showWithParen(); public void printStrong() banner.showWithAster(); ,Adapter模式,Implementation 使用C+繼承機制實現(xiàn)class

9、 adapter 使用內(nèi)嵌對象技術(shù)實現(xiàn)object adapter Pluggable adapters,三種實現(xiàn)方案 使用抽象方法定義 使用代理對象 參數(shù)化技術(shù) 這三種方法的實質(zhì):如何在一個類中定義抽象操作,供客戶插入? hook 技術(shù),例子:動物圖形編輯器,我們的程序,現(xiàn)成的程序,重用,策略1對象適配器,適配器 采用對象復(fù)合,策略1對象適配器,/這是個適配器程序,適配Tiger使其具有Tookit的接口 public class TigerAdaptor implements Tookit public Tiger theTiger; public TigerAdaptor(Tiger t

10、) theTiger=t; public void draw() theTiger.paint(); ,策略2類適配器,適配器 采用類繼承,策略2采用繼承,public class TigerAdaptor1 extends Tiger implements Tookit public TigerAdaptor1() public void draw() super.paint(); ,例子,Java.util.Properties類是用來管理鍵盤和相對應(yīng)數(shù)值的。該類有兩個方法: void load (InputStream in) throws IOException 從輸入流中讀取內(nèi)容。

11、void store(OutputStream out,String header) throws IOException 把內(nèi)容寫入輸出流。 問題:利用這個類建立一個把內(nèi)容的集合存儲成文件的FileProperties類。假設(shè)FileIO接口約束FileProperties應(yīng)具有的方法。,import java.io.*; public interface FileIO public void readFromFile(String filename) throws IOException; public void writeToFile(String filename) throws IO

12、Exception; public void setValue(String key, String value); public String getValue(String key); ,import java.io.*; import java.util.*; public class FileProperties extends Properties implements FileIO public void readFromFile(String filename) throws IOException load(new FileInputStream(filename); publ

13、ic void writeToFile(String filename) throws IOException store(new FileOutputStream(filename), written by FileProperties); public void setValue(String key, String value) setProperty(key, value); public String getValue(String key) return getProperty(key, ); ,public static void main(String args) / TODO code application logic here FileIO f = new FileProperties(); try f.readFromFile(file.txt); Syst

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論