1、功能
适配器模式把一个类的接口变换成客户端所期待的另一种接口,从而使原本因接口不匹配而无法在一起工作的两个类能够在一起工作。
2、模式中的角色
适配器模式有类的适配器模式和对象的适配器模式两种不同的形式,还有就是缺省适配模式。
3、缺省适配模式
缺省适配(Default Adapter)模式为一个接口提供缺省实现,这样子类型可以从这个缺省实现进行扩展,而不必从原有接口进行扩展。作为适配器模式的一个特例,缺省适配模式在JAVA语言中有着特殊的应用。
我们在SE开发中经常使用界面的关闭事件。
public TestWindowListener2() { setLayout(new FlowLayout(FlowLayout.CENTER)); add(new JButton("我是一个按钮")); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e)//窗口正处在关闭过程中时调用 { System.out.println("我关"); System.exit(0); } } ); } |
WindowListener接口定义了一序列窗口事件。WindowAdapter抽象类实现了接口WindowListener的windowClosing 抽象方法的空实现(默认实现)public void windowClosing(WindowEvent e) {},这样客户端只需要继承适配器类,重写关注的接口代码即可!而不需要实现接口定义的每个方法。在上面案例中windowClosing为我们需要关注的方法重写该方法的实现即可,而不需要强制实现接口定义的其他方法比如windowOpened事件。
类的适配器模式和对象适配器模式可以参考博文http://www.cnblogs.com/java-my-life/archive/2012/04/13/2442795.html,个人觉得写的还是比较清楚的。
4、类的适配器模式
类的适配器模式把适配的类的API转换成为目标类的API。
在上图中可以看出,Adaptee类并没有sampleOperation2()方法,而客户端则期待这个方法。为使客户端能够使用Adaptee类,提供一个中间环节,即类Adapter,把Adaptee的API与Target类的API衔接起来。Adapter与Adaptee是继承关系,这决定了这个适配器模式是类的:
模式所涉及的角色有:
● 目标(Target)角色:这就是所期待得到的接口。注意:由于这里讨论的是类适配器模式,因此目标不可以是类。
● 源(Adapee)角色:现在需要适配的接口。
● 适配器(Adaper)角色:适配器类是本模式的核心。适配器把源接口转换成目标接口。显然,这一角色不可以是接口,而必须是具体类。
public interface Target { /** * 这是源类Adaptee也有的方法 */ public void sampleOperation1(); /** * 这是源类Adapteee没有的方法 */ public void sampleOperation2(); } |
上面给出的是目标角色的源代码,这个角色是以一个JAVA接口的形式实现的。可以看出,这个接口声明了两个方法:sampleOperation1()和sampleOperation2()。而源角色Adaptee是一个具体类,它有一个sampleOperation1()方法,但是没有sampleOperation2()方法。
public class Adaptee { } |
适配器角色Adapter扩展了Adaptee,同时又实现了目标(Target)接口。由于Adaptee没有提供sampleOperation2()方法,而目标接口又要求这个方法,因此适配器角色Adapter实现了这个方法。
public class Adapter extends Adaptee implements Target { } |
5、对象适配器模式
与类的适配器模式一样,对象的适配器模式把被适配的类的API转换成为目标类的API,与类的适配器模式不同的是,对象的适配器模式不是使用继承关系连接到Adaptee类,而是使用委派关系连接到Adaptee类。
从上图可以看出,Adaptee类并没有sampleOperation2()方法,而客户端则期待这个方法。为使客户端能够使用Adaptee类,需要提供一个包装(Wrapper)类Adapter。这个包装类包装了一个Adaptee的实例,从而此包装类能够把Adaptee的API与Target类的API衔接起来。Adapter与Adaptee是委派关系,这决定了适配器模式是对象的。
public interface Target { /** * 这是源类Adaptee也有的方法 */ public void sampleOperation1(); /** * 这是源类Adapteee没有的方法 */ public void sampleOperation2(); } |
public class Adaptee { public void sampleOperation1(){} |
public class Adapter { private Adaptee adaptee; public Adapter(Adaptee adaptee){ this.adaptee = adaptee; } /** * 源类Adaptee有方法sampleOperation1 * 因此适配器类直接委派即可 */ public void sampleOperation1(){ this.adaptee.sampleOperation1(); } /** * 源类Adaptee没有方法sampleOperation2 * 因此由适配器类需要补充此方法 */ public void sampleOperation2(){ //写相关的代码 } } |