软件设计与体系结构课件15适配器模式

上传人:E**** 文档编号:91205683 上传时间:2019-06-26 格式:PPT 页数:37 大小:337KB
返回 下载 相关 举报
软件设计与体系结构课件15适配器模式_第1页
第1页 / 共37页
软件设计与体系结构课件15适配器模式_第2页
第2页 / 共37页
软件设计与体系结构课件15适配器模式_第3页
第3页 / 共37页
软件设计与体系结构课件15适配器模式_第4页
第4页 / 共37页
软件设计与体系结构课件15适配器模式_第5页
第5页 / 共37页
点击查看更多>>
资源描述

《软件设计与体系结构课件15适配器模式》由会员分享,可在线阅读,更多相关《软件设计与体系结构课件15适配器模式(37页珍藏版)》请在金锄头文库上搜索。

1、软件设计与体系结构 适配器模式 主讲:李健利,适配器模式,意图 =将一个类的接口转换成客户希望的另外一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的那些类可以一起工作。 别名 =包装器(Wrapper) 动机 =有时,工具箱或类库不能够被使用的原因是因为其接口与应用程序所需要的接口不兼容。 =我们不能更改库接口,因为我们可能没有它的源代码。 =即使我们拥有源代码,可能我们也不应为每个与领域相关的应用程序而更改库,适配器模式,动机 =实例:,适配器模式,结构 =类适配器使用多重继承将一个接口适配为另一个接口:,适配器模式,结构 =对象匹配器依赖于对象组合:,适配器模式,适配器模式,适用

2、性 以下情况使用适配器模式 =你想使用一个已经存在的类,而其接口不符合你的需求。 =你想创建一个可复用的类,该类可以与其他一些接口不兼容的不相关类协作 实现问题 =应完成的适配程度 简单的接口转换。只改变操作名称和参数顺序 完全不同的操作集 =适配器是否提供双向透明 双向适配器支持Target 和Adaptee两个接口。它允许被适配的对象(适配器)似乎是一个Adaptee 对象或Target对象,适配器模式例1,经典的圆柱和方柱问题! 以下是SquarePeg类: /* * The SquarePeg class. * This is the Target class. */ public c

3、lass SquarePeg public void insert(String str) System.out.println(“SquarePeg insert(): “ + str); ,适配器模式例1,下面是RoundPeg类: /* * The RoundPeg class. * This is the Adaptee class. */ public class RoundPeg public void insertIntoHole(String msg) System.out.println(“RoundPeg insertIntoHole(): “ + msg); 如果只理解使

4、用insert() 方法插入柱子的SquarePeg接口,那么该 接口如何能插入圆柱那?柱子适配器!,适配器模式例1 (续),下面是PegAdapter类: /* * The PegAdapter class. * This is the Adapter class. * It adapts a RoundPeg to a SquarePeg. * Its interface is that of a SquarePeg. */ public class PegAdapter extends SquarePeg private RoundPeg roundPeg; public PegAdap

5、ter(RoundPeg peg) this.roundPeg = peg; public void insert(String str) roundPeg.insertIntoHole(str); ,适配器模式例1 (续),典型的客户端程序: / Test program for Pegs. public class TestPegs public static void main(String args) / Create some pegs. RoundPeg roundPeg = new RoundPeg(); SquarePeg squarePeg = new SquarePeg()

6、; / Do an insert using the square peg. squarePeg.insert(“Inserting square peg.“);,适配器模式例1 (续),/ Now wed like to do an insert using the round peg. / But this client only understands the insert() / method of pegs, not a insertIntoHole() method. / The solution: create an adapter that adapts / a square

7、peg to a round peg! PegAdapter adapter = new PegAdapter(roundPeg); adapter.insert(“Inserting round peg.“); 客户端程序输出: SquarePeg insert(): Inserting square peg. RoundPeg insertIntoHole(): Inserting round peg.,适配器模式例2,注意在例1中, PegAdapter将RoundPeg适配为SquarePeg。 PegAdapter的接口就是SquarePeg的接口。 如果我们想要有一个既能作为Squ

8、arePeg又能作为RoundPeg的适配器该怎么办?这种适配器称作双向适配器。 实现双向适配器的一种方式是使用多重继承,但在Java中做不到。 但是我们能拥有实现两个不同Java接口的适配器!,适配器模式例2 (续),下面是圆柱和方柱接口: /* *The IRoundPeg interface. */ public interface IRoundPeg public void insertIntoHole(String msg); /* *The ISquarePeg interface. */ public interface ISquarePeg public void insert

9、(String str); ,适配器模式例2 (续),下面是新的RoundPeg和SquarePeg类。除了这些类现在实现适当接口外,它们基本上是相同的。 / The RoundPeg class. public class RoundPeg implements IRoundPeg public void insertIntoHole(String msg) System.out.println(“RoundPeg insertIntoHole(): “ + msg); / The SquarePeg class. public class SquarePeg implements ISqu

10、arePeg public void insert(String str) System.out.println(“SquarePeg insert(): “ + str); ,适配器模式例2 (续),下面是新的PegAdapter: /* * The PegAdapter class. * This is the two-way adapter class. */ public class PegAdapter implements ISquarePeg, IRoundPeg private RoundPeg roundPeg; private SquarePeg squarePeg; pu

11、blic PegAdapter(RoundPeg peg) this.roundPeg = peg; public PegAdapter(SquarePeg peg) this.squarePeg = peg; public void insert(String str) roundPeg.insertIntoHole(str); public void insertIntoHole(String msg)squarePeg.insert(msg); ,适配器模式例2 (续),使用双向适配器的客户端: / Test program for Pegs. public class TestPegs

12、 public static void main(String args) / Create some pegs. RoundPeg roundPeg = new RoundPeg(); SquarePeg squarePeg = new SquarePeg(); / Do an insert using the square peg. squarePeg.insert(“Inserting square peg.“); / Create a two-way adapter and do an insert with it. ISquarePeg roundToSquare = new Peg

13、Adapter(roundPeg); roundToSquare.insert(“Inserting round peg.“);,适配器模式例2 (续),/ Do an insert using the round peg. roundPeg.insertIntoHole(“Inserting round peg.“); / Create a two-way adapter and do an insert with it. IRoundPeg squareToRound = new PegAdapter(squarePeg); squareToRound.insertIntoHole(“In

14、serting square peg.“); 客户端程序输出: SquarePeg insert(): Inserting square peg. RoundPeg insertIntoHole(): Inserting round peg. RoundPeg insertIntoHole(): Inserting round peg. SquarePeg insert(): Inserting square peg.,适配器模式例3,该例来自圣迭戈州立大学的Roger Whitney 情形: 存在用于创建CGI web 服务器程序的Java类库。库中的一个类是CGIVariables类,它将

15、所有的CGI环境变量存入一个散列表,且允许通过get(String evName) 方法访问它们。很多Java CGI程序已使用该库编写。 Web服务器的最新版本支持servlet,它提供的功能类似于CGI程序,但其效率要高得多。 Servlet库有一个HttpServletRequest 类,该类有一个针对每个CGI环境变量的getX()方法。我们想使用servlet。我们是否应重写所有的现存Java CGI 程序?,适配器模式例3,解决方案:我们必须重写一些代码,但要尝试尽量减少。我们能够设计一个CGIAdapter 类,它具有与原来的CGIVariables类相同的接口(get() me

16、thod) ,但它在HttpServletRequest类外加上一个包装器。 CGI 程序现在必须使用该CGIAdapter类而不是原来的CGIVariables类,但get() 方法调用的形式不必改变。,适配器模式例3 (续),下面是CGIAdapter类的片段: public class CGIAdapter Hashtable CGIVariables = new Hashtable(20); public CGIAdapter(HttpServletRequest CGIEnvironment) CGIVariables.put(“AUTH_TYPE“, CGIEnvironment.getAuthType(); CGIVariables.put(“REMOTE_USER“, CGIEnvironment.getRemoteUser(); etc. public

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 高等教育 > 大学课件

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号