当前位置:文档之家› 适配器模式(Adapter)

适配器模式(Adapter)

2012年3月
UML与设计模式
13/35
适配器(Adapter)设计模式
问题 新旧接口的转换 解决方案 适配器实现新的接口 对旧接口 继承:类适配器 组合:对象适配器 使用 声明新接口的引用
2012年3月
UML与设计模式
14/35
说明:
实现Adapter方式,有两种方式:组合(composition) 和继承(inheritance). 转换一个类的接口为客户端所需要的接口,将两 个不兼容的类纠合在一起使用,这个转换的类就是 适配器类。它属于结构型模式,需要有Adaptee(被 适配者)和Adapter(适配器)两个身份.它的宗旨就是, 保留现有类所提供的服务,向客户提供接口,以满足 客户的期望。
}
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(); } }
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
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月
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…
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(); } }
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(); }
第二章 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与设计模式
21/35
示例二:对象适配器
Main Banner +showWithParen() +showWithAster() <<uses>> 1 1 <<接口>> Print +PrintWeak() +PrintStrong() PrintBanner +printWeak() +printStrong()
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与设计模式
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
8/35
2012年3月
UML与设计模式
适配器(Adapter)设计模式
如果你有一个存在的系统需要插入一个新的类库, 但是新的类库并不能匹配你写的系统,如下图:
相关主题