适配器模式(Adapter)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

}
2012年3月
UML与设计模式
19/35
类适配器
public class PrintBanner extends Banner implements Print{ public PrintBanner(String str){ super(str); } public void printWeak(){ showWithParen(); } public void printStrong(){ showWithAster(); } }
In Short, a solution for a typical problem
2012源自文库3月
UML与设计模式
2/35
Why do we need Patterns?
Reusing design knowledge Problems are not always unique. Reusing existing experience might be useful. Patterns give us hints to “where to look for problems”. Establish common terminology Easier to say, "We need a Façade here“. Provide a higher level prospective Frees us from dealing with the details too early In short, it’s a “reference”
UML与设计模式
2012年3月
3/35
History of Design Patterns
Christopher Alexander
The Timeless Way of Building A Pattern Language: Towns, Buildings, Construction
Architecture
2012年3月
UML与设计模式
5/35
Gang of Four (GoF) patterns
Creational Patterns (concerned with abstracting the object-instantiation process) Factory Method Abstract Factory Singleton Builder Prototype Structural Patterns (concerned with how objects/classes can be combined to form larger structures) Adapter Bridge Composite Decorator Facade Flyweight Proxy Behavioral Patterns (concerned with communication between objects) Command Interpreter Iterator Mediator Observer State Strategy Chain of Responsibility Visitor Template Method memento 2012年3月
2012年3月
UML与设计模式
21/35
示例二:对象适配器
Main Banner +showWithParen() +showWithAster() <<uses>> 1 1 <<接口>> Print +PrintWeak() +PrintStrong() PrintBanner +printWeak() +printStrong()
UML与设计模式
18/35
类适配器
public interface Print{ public abstract void printWeak(); public abstract void printStrong(); } public class Banner{
private String str; public Banner(String str){ this.str = str; } public void showWithParen(){ System.out.println("(" + str + ")"); } public void showWithAster(){ System.out.println("*" + str + "*"); }
2007’
2012年3月
UML与设计模式
4/35
Types of Design Patterns
Creational Deal with the best way to create objects Structural Ways to bring together groups of objects Behavioral Ways for objects to communicate & interact
UML与设计模式
17/35
示例一:类适配器
Main Banner +showWithParen() +showWithAster() <<uses>>
<<接口>> Print +PrintWeak() +PrintStrong()
2012年3月
PrintBanner +printWeak() +printStrong()
2012年3月
UML与设计模式
22/35
public class PrintBanner implements Print{ private Banner banner; public PrintBanner(String str){ this.banner = new Banner(str); } public void printWeak(){ banner.showWithParen(); } public void printStrong(){ banner.showWithAster(); } } public class Test{ public static void main(String args[]){ Print p = new PrintBanner("Hello"); p.printWeak(); p.printStrong(); }
2012年3月
UML与设计模式
10/35
适配器模式
用了适配器之后的整个系统,如下图:
2012年3月
UML与设计模式
11/35
Incompatibility problems
We may have an application that needs to use libraries/a different application/you-name-it, but the thing we want to call has a different interface than our caller Alternative 1: Re-write the caller Ugly, messy, error-prone Alternative 2: Re-write the called libraries/classes May not have the source code As ugly and error-prone as Alternative 1 Alternative 3: Write an adapter The adapter converts all requests to a language the adaptee understands
第二章 UML-类图
Design Patterns Introduction
UML与设计模式
结构型模式
Adapter模式
代理模式
2012年3月
UML与设计模式
1/35
What is a Design Pattern?
A description of a recurrent problem and of the core of possible solutions.
2012年3月
UML与设计模式
20/35
类适配器
public class Test{ public static void main(String args[]){ Print p = new PrintBanner("Hello"); p.printWeak(); p.printStrong(); ((PrintBanner)p).showWithParen(); ((PrintBanner)p).showWithAster(); } }
8/35
2012年3月
UML与设计模式
适配器(Adapter)设计模式
如果你有一个存在的系统需要插入一个新的类库, 但是新的类库并不能匹配你写的系统,如下图:
2012年3月
UML与设计模式
9/35
适配器模式
现在你不想更改存在的旧系统,新的类库也不能修 改,这时候我们就需要写一个适配器了,用这个适配 器来适配新类库的接口。如下图:
UML与设计模式
6/35
GoF结构型模式
关注类/对象之间的结构 主要的结构型模式: 外观 为子系统提供一个入口点 代理 为对象提供一个代理来控制对该对象的访问 适配器 利用现有类所提供的服务,实现用户所需要 的接口
2012年3月
UML与设计模式
7/35
复合 可以用统一的接口处理有层次结构的对象 装饰 可以动态组织对象的行为 其它模式: 桥接 将抽象与抽象操作的实现分离开,使抽象与实 现可以独立变化 共享元 通过共享为大量的细粒度对象提供有效的支持
2012年3月
UML与设计模式
13/35
适配器(Adapter)设计模式
问题 新旧接口的转换 解决方案 适配器实现新的接口 对旧接口 继承:类适配器 组合:对象适配器 使用 声明新接口的引用
2012年3月
UML与设计模式
14/35
说明:
实现Adapter方式,有两种方式:组合(composition) 和继承(inheritance). 转换一个类的接口为客户端所需要的接口,将两 个不兼容的类纠合在一起使用,这个转换的类就是 适配器类。它属于结构型模式,需要有Adaptee(被 适配者)和Adapter(适配器)两个身份.它的宗旨就是, 保留现有类所提供的服务,向客户提供接口,以满足 客户的期望。
2012年3月
UML与设计模式
15/35
Adapter
Client ClientInterface Request() LegacyClass ExistingRequest()
adaptee Adapter Request()
2012年3月
UML与设计模式
16/35
Adapter类图
2012年3月
2012年3月
UML与设计模式
12/35
The Adapter pattern
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces Applicability: Use 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 don’t necessarily have compatible interfaces
1970’
Gang of Four (GoF)
Design Patterns: Elements of Reusable Object-Oriented Software
Object Oriented Software Design Other Areas:
1995’
Many Authors
HCI, Organizational Behavior, Education, Concurrent Programming…
相关文档
最新文档