设计模式10——Adapter设计模式
2014-09-18 09:55
176 查看
1、Adapter适配器模式是将两个不兼容的类组合在一起使用。生活中笔记本电脑和手机等数码产品的充电器就是一个适配器,将家用220V的交流电转换为笔记本或手机正常工作所需的目标电压和电流。适配器起到一种转换和包装的作用。
2、Adapter设计模式主要目的组合两个不相干类,常用有两种方法:
1)第一种解决方案是修改各自类的接口。
2)但是如果没有源码,或者不愿意为了一个应用而修改各自的接口,则需要使用Adapter适配器,在两种接口之间创建一个混合接口。
3、Adapter适配器设计模式中有3个重要角色:被适配者Adaptee,适配器Adapter和目标对象Target。其中两个现存的想要组合到一起的类分别是被适配者Adaptee和目标对象Target角色,我们需要创建一个适配器Adapter将其组合在一起。
4、实现Adapter适配器设计模式有两种方式:组合(compositon, has-a关系)和继承(inheritance,is-a关系)。
对象适配器模式使用组合,UML图如下:
![](http://img.my.csdn.net/uploads/201211/18/1353228050_7063.png)
类适配器模式使用继承,UML图如下:
![](http://img.my.csdn.net/uploads/201211/18/1353228042_1964.png)
假设要画图形,有圆形和方形两个类,现在需要一个既可以画圆形又可以画方形的类。
组合关系的Adapter适配器模式如下:
[java] view
plaincopy
//圆形,目标对象
class Cirecle{
public void drawCircle(){
System.out.println(“Draw circle”);
}
}
//方形,被适配对象
class Square{
public void drawSquare(){
System.out.println(“Draw square”);
}
}
//既可以画圆形,又可以画方形,适配器
public class HybridShape extends Circle{
private Square square;
public HybridShape(Square square){
this.square = square;
}
public void drawSquare(){
square.drawSquare();
}
}
适配器继承目标对象,同时组合被适配对象,如果需要画圆时,直接调用父类的画圆方法即可,如果需要画方,则调用被适配对象的画方形方法。
继承关系的Adapter适配器模式如下:
[java] view
plaincopy
interface ICircle{
public void drawCircle();
}
interface ISquare{
public void drawSquare();
}
//圆形
class Cirecle implements ICircle{
public void drawCircle(){
System.out.println(“Draw circle”);
}
}
//方形
class Square implements ISquare{
public void drawSquare(){
System.out.println(“Draw square”);
}
}
//既可以画圆形,又可以画方形,适配器
public class HybridShape implements ICircle, ISquare{
private ISquare square;
private ICircle circle;
public HybridShape(Square square){
this.square = square;
}
public HybridShape(Circle circle){
this.circle = circle;
}
public void drawSquare(){
square.drawSquare();
}
public void drawCircle(){
circle.drawCircle();
}
}
Java中类不允许多继承,但是可以实现多个接口,继承关系的Adapter适配器设计模式就是利用java可以实现多个接口的特性。
JDK里面的适配器模式应用:
•java.util.Arrays#asList()
•java.io.InputStreamReader(InputStream)
•java.io.OutputStreamWriter(OutputStream)
•javax.xml.bind.annotation.adapters.XmlAdapter#marshal()
•javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal()
2、Adapter设计模式主要目的组合两个不相干类,常用有两种方法:
1)第一种解决方案是修改各自类的接口。
2)但是如果没有源码,或者不愿意为了一个应用而修改各自的接口,则需要使用Adapter适配器,在两种接口之间创建一个混合接口。
3、Adapter适配器设计模式中有3个重要角色:被适配者Adaptee,适配器Adapter和目标对象Target。其中两个现存的想要组合到一起的类分别是被适配者Adaptee和目标对象Target角色,我们需要创建一个适配器Adapter将其组合在一起。
4、实现Adapter适配器设计模式有两种方式:组合(compositon, has-a关系)和继承(inheritance,is-a关系)。
对象适配器模式使用组合,UML图如下:
![](http://img.my.csdn.net/uploads/201211/18/1353228050_7063.png)
类适配器模式使用继承,UML图如下:
![](http://img.my.csdn.net/uploads/201211/18/1353228042_1964.png)
假设要画图形,有圆形和方形两个类,现在需要一个既可以画圆形又可以画方形的类。
组合关系的Adapter适配器模式如下:
[java] view
plaincopy
//圆形,目标对象
class Cirecle{
public void drawCircle(){
System.out.println(“Draw circle”);
}
}
//方形,被适配对象
class Square{
public void drawSquare(){
System.out.println(“Draw square”);
}
}
//既可以画圆形,又可以画方形,适配器
public class HybridShape extends Circle{
private Square square;
public HybridShape(Square square){
this.square = square;
}
public void drawSquare(){
square.drawSquare();
}
}
适配器继承目标对象,同时组合被适配对象,如果需要画圆时,直接调用父类的画圆方法即可,如果需要画方,则调用被适配对象的画方形方法。
继承关系的Adapter适配器模式如下:
[java] view
plaincopy
interface ICircle{
public void drawCircle();
}
interface ISquare{
public void drawSquare();
}
//圆形
class Cirecle implements ICircle{
public void drawCircle(){
System.out.println(“Draw circle”);
}
}
//方形
class Square implements ISquare{
public void drawSquare(){
System.out.println(“Draw square”);
}
}
//既可以画圆形,又可以画方形,适配器
public class HybridShape implements ICircle, ISquare{
private ISquare square;
private ICircle circle;
public HybridShape(Square square){
this.square = square;
}
public HybridShape(Circle circle){
this.circle = circle;
}
public void drawSquare(){
square.drawSquare();
}
public void drawCircle(){
circle.drawCircle();
}
}
Java中类不允许多继承,但是可以实现多个接口,继承关系的Adapter适配器设计模式就是利用java可以实现多个接口的特性。
JDK里面的适配器模式应用:
•java.util.Arrays#asList()
•java.io.InputStreamReader(InputStream)
•java.io.OutputStreamWriter(OutputStream)
•javax.xml.bind.annotation.adapters.XmlAdapter#marshal()
•javax.xml.bind.annotation.adapters.XmlAdapter#unmarshal()
相关文章推荐
- C#设计模式(10)-Adapter Pattern
- 设计模式 (10)——适配器模式(Adapter Pattern,结构型)
- 设计模式10——Adapter设计模式--http://blog.csdn.net/chjttony?viewmode=contents
- C#设计模式(10)-Adapter Pattern
- 设计模式10——Adapter设计模式
- 设计模式10——Adapter设计模式
- 设计模式10—Adapter设计模式
- 设计模式(10)-适配器Adapter
- 设计模式笔记(10 MEDIATOR & MEMENTO)
- 设计模式袖珍版 连续转载之 - Adapter(适配器)
- Java设计模式:Adapter(适配器)
- [导入]设计模式adapter
- .NET设计模式(10):装饰模式(Decorator Pattern)
- 设计模式笔记(4 ADAPTER & BRIDGE)
- C#设计模式(10)-Adapter Pattern
- 设计模式笔记(4 ADAPTER & BRIDGE)
- 模式设计(七)Adapter
- “凿壁借光”的模式场景——设计模式系列谈之Adapter模式
- .NET设计模式(10):装饰模式(Decorator Pattern)
- 设计模式之适配器(Adapter)---类对象结构型模式