适配器(Adaptor)模式.ppt_第1页
适配器(Adaptor)模式.ppt_第2页
适配器(Adaptor)模式.ppt_第3页
适配器(Adaptor)模式.ppt_第4页
适配器(Adaptor)模式.ppt_第5页
已阅读5页,还剩16页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、适配器(Adaptor)模式,目的: 把一个已有的类用用户希望的形式描述;即用用户希望的接口适配这个类。 在这个模式中,这个类叫做被适配者(Adaptee),用户定义的接口叫适配接口,用户实现适配接口的类叫适配器(Adaptor)。也叫做,包装器(Wrapper)。,Adapter,假设有一图形编辑器,允许用户绘制和排列基本图元生成复杂图形。 该程序中有一个接口:Shape,定义了图形对象的基本接口服务。程序中所有图元对象均实现该接口。 假设需要设计一个TextShape类,用来显示和编辑正文。设计该类时希望用到现成的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. 你想创建一个可以复用的类,该类可以与其他不相关的类或不可预见的类(即那些接口可能不兼容的类)协同工作。 (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+继承机制实现class

9、 adapter 使用内嵌对象技术实现object adapter Pluggable adapters,三种实现方案 使用抽象方法定义 使用代理对象 参数化技术 这三种方法的实质:如何在一个类中定义抽象操作,供客户插入? hook 技术,例子:动物图形编辑器,我们的程序,现成的程序,重用,策略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类是用来管理键盘和相对应数值的。该类有两个方法: 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. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论