(三) 适配器设计模式

  • 类的适配器模式
  • 对象的适配器模式
  • 接口的适配器模式

思想:

将类的某个接口转换成客户端期望的另一个接口表示

目的:

消除由于接口不匹配造成的类的兼容性问题

1.UML图之类的适配器设计模式

public class Source {  
2.  
3.    public void method1() {  
4.        System.out.println("this is original method!");  
5.    }  
6.}  
[java] view plaincopy 
1.public interface Targetable {  
2.  
3.    /* 与原类中的方法相同 */  
4.    public void method1();  
5.  
6.    /* 新类的方法 */  
7.    public void method2();  
8.}  
[java] view plaincopy 
1.public class Adapter extends Source implements Targetable {  
2.  
3.    @Override  
4.    public void method2() {  
5.        System.out.println("this is the targetable method!");  
6.    }  
7.}  

Adapter类继承Source类,实现Targetable接口,下面是测试类:

[java] view plaincopy 
1.public class AdapterTest {  
2.  
3.    public static void main(String[] args) {  
4.        Targetable target = new Adapter();  
5.        target.method1();  
6.        target.method2();  
7.    }  
8.}  
示例代码

2.UML图之对象的适配器设计模式

 

只需要修改Adapter类的源码即可:

[java] view plaincopy 
1.public class Wrapper implements Targetable {  
2.  
3.    private Source source;  
4.      
5.    public Wrapper(Source source){  
6.        super();  
7.        this.source = source;  
8.    }  
9.    @Override  
10.    public void method2() {  
11.        System.out.println("this is the targetable method!");  
12.    }  
13.  
14.    @Override  
15.    public void method1() {  
16.        source.method1();  
17.    }  
18.}  

测试类:

[java] view plaincopy 
1.public class AdapterTest {  
2.  
3.    public static void main(String[] args) {  
4.        Source source = new Source();  
5.        Targetable target = new Wrapper(source);  
6.        target.method1();  
7.        target.method2();  
8.    }  
9.}  
示例代码

 

 

3.UML图之接口的适配器设计模式

 有时我们写的一个接口中有多个抽象方法,当我们写该接口的实现类时,必须实现该接口的所有方法,

这明显有时比较浪费,因为并不是所有的方法都是我们需要的,有时只需要某一些,此处为了解决这个问题,

我们引入了接口的适配器模式,借助于一个抽象类,该抽象类实现了该接口,实现了所有的方法,

而我们不和原始的接口打交道,只和该抽象类取得联系,所以我们写一个类,继承该抽象类,重写我们需要的方法就行

 

//定义接口
public interface Sourceable {  
2.      
3.    public void method1();  
4.    public void method2();  
5.}  

//抽象类Wrapper2,实现接口:
1.public abstract class Wrapper2 implements Sourceable{  
2.      
3.    public void method1(){}  
4.    public void method2(){}  
5.}  

//Sub1继承抽象类
1.public class SourceSub1 extends Wrapper2 {  
2.    public void method1(){  
3.        System.out.println("the sourceable interface's first Sub1!");  
4.    }  
5.}
 //Sub2继承抽象类
1.public class SourceSub2 extends Wrapper2 {  
2.    public void method2(){  
3.        System.out.println("the sourceable interface's second Sub2!");  
4.    }  
5.}  
//测试类
1.public class WrapperTest {  
2.  
3.    public static void main(String[] args) {  
4.        Sourceable source1 = new SourceSub1();  
5.        Sourceable source2 = new SourceSub2();  
6.          
7.        source1.method1();  
8.        source1.method2();  
9.        source2.method1();  
10.        source2.method2();  
11.    }  
12.}  

运行结果:
the sourceable interface's first Sub1!
the sourceable interface's second Sub2!
View Code
 
 
小结

类的适配器模式当希望将一个类转换成满足另一个新接口的类时,可以使用类的适配器模式,创建一个新类,继承原有的类,实现新的接口即可。

对象的适配器模式当希望将一个对象转换成满足另一个新接口的对象时,可以创建一个Wrapper类,持有原类的一个实例,在Wrapper类的方法中,调用实例的方法就行。

接口的适配器模式:当不希望实现一个接口中所有的方法时,可以创建一个抽象类Wrapper,实现所有方法,我们写别的类的时候,继承抽象类即可。

 
 

 

posted @ 2017-05-09 19:00  EugeniaGao  阅读(196)  评论(0编辑  收藏  举报