




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
1、適配器(Adaptor)模式,目的: 把一個已有的類用用戶希望的形式描述;即用用戶希望的接口適配這個類。 在這個模式中,這個類叫做被適配者(Adaptee),用戶定義的接口叫適配接口,用戶實現(xiàn)適配接口的類叫適配器(Adaptor)。也叫做,包裝器(Wrapper)。,Adapter,假設有一圖形編輯器,允許用戶繪制和排列基本圖元生成復雜圖形。 該程序中有一個接口:Shape,定義了圖形對象的基本接口服務。程序中所有圖元對象均實現(xiàn)該接口。 假設需要設計一個TextShape類,用來顯示和編輯正文。設計該類時希望用到現(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. 你想使用一個已經存在的類,并且它的接口不符合要求。 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)建一個可以復用的類,該類可以與其他不相關的類或不可預見的類(即那些接口可能不兼容的類)協(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.你想使用一些已經存在的子類,但是不可能對每一個都進行子類化以匹配它們的接口。對象適配器
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的實例,在其內部通過產生的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 使用內嵌對象技術實現(xiàn)object adapter Pluggable adapters,三種實現(xiàn)方案 使用抽象方法定義 使用代理對象 參數(shù)化技術 這三種方法的實質:如何在一個類中定義抽象操作,供客戶插入? hook 技術,例子:動物圖形編輯器,我們的程序,現(xiàn)成的程序,重用,策略1對象適配器,適配器 采用對象復合,策略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類是用來管理鍵盤和相對應數(shù)值的。該類有兩個方法: void load (InputStream in) throws IOException 從輸入流中讀取內容。
11、void store(OutputStream out,String header) throws IOException 把內容寫入輸出流。 問題:利用這個類建立一個把內容的集合存儲成文件的FileProperties類。假設FileIO接口約束FileProperties應具有的方法。,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)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 關于安全生產的培訓總結
- 交通事故賠償律師哪個好
- 月安全生產總結及下月安全工作的計劃
- 食品安全工作述職報告
- 角磨機安全操作規(guī)程完整版
- 煤礦項目經理安全生產責任制
- 2025年中國板式單面實驗臺行業(yè)市場發(fā)展前景及發(fā)展趨勢與投資戰(zhàn)略研究報告
- 玉米栽培管理技術課件
- 玉蘭花介紹課件
- 湖北省宜昌市二中2025屆高一下化學期末經典試題含解析
- GB/T 29790-2020即時檢驗質量和能力的要求
- 深圳市失業(yè)人員停止領取失業(yè)保險待遇申請表空表
- 《生理學》神經系統(tǒng)課件
- 傲鵬ERP應付會計操作培訓課件
- 催收投訴防控預警處理流程(含投訴預警報備臺賬)
- 硬筆書法:幼小銜接識字寫字教學課件
- 公開招聘校長后備人選理論考試題庫
- 機械優(yōu)化設計_經典實例PPT課件
- 新人教版八年級物理(下冊) 第十一章 功和機械能 第十一章 功與機械能復習課
- 東方航空無成人陪伴兒童乘機申請書
- 智慧工廠解決方案—燈塔工廠引領制造業(yè)數(shù)字化轉型-白皮書
評論
0/150
提交評論