适配器设计模式
现实生活中的适配器例子
泰国插座用的是两孔的(欧标),可以买个多功能转换插头(适配器),这样就可以使用了。
基本介绍
1)适配器模式(AdapterPattern)将某个类的接口转换成客户端期望的另一个接口表示,主要目的是兼容性,让原本因接口不匹配不能一起工作的两个类可以协同工作。其别名为包装器(Wrapper)
2)适配器模式属于结构型模式
3)主要分为三类:类适配器模式、对象适配器模式、接口适配器模式
工作原理
1)适配器模式:将一个类的接口转换成另一个接口,让原本接口不兼容的类可以兼容
2)从用户角度看不到被是适配者,是解耦的
3)用户调用适配器转化出来的目标方法,适配器再调用被适配者的相关接口方法
4)用户收到反馈结果,感觉只是和目标接口交互,如图
1、类适配器模式
类适配器模式介绍
基本介绍:Adapter类,通过继承src类,实现dst接口,完成src>dst的适配。
类适配器模式应用实例
1)应用实例说明:
以生活中充电器的例子来讲解适配器,充电器本身相当于Adapter,220V交流点相当于src(即叫适配者),我们的目的dst(即目标)是5V直流电
2)思路分析(类图)
package designPatterns.adapter.type1;
//220v的电压
public class Voltage220 {
private Integer vol;
public Voltage220() {
this.vol = 220;
}
public Integer output200() {
System.out.println("电压220v");
return this.vol;
}
public Integer getVol() {
return vol;
}
public void setVol(Integer vol) {
this.vol = vol;
}
}
package designPatterns.adapter.type1;
//电压为5v接口
public interface IVoltage5 {
Integer output5();
}
package designPatterns.adapter.type1;
public class VoltageAdapter extends Voltage220 implements IVoltage5{
@Override
public Integer output5() {
int vol220 = super.getVol();//电压为200
int vol5=vol220/44;//将电压转为5V
return vol5;
}
}
package designPatterns.adapter.type1;
//手机
public class Phone {
//手机充电
public void charging(IVoltage5 iVoltage5){
Integer output5 = iVoltage5.output5();
if (output5==5){
System.out.println("当前电压为5V可以充电");
}else if(output5>5){
System.out.println("当前电压大于5V,手机爆炸");
}
}
}
package designPatterns.adapter.type1;
public class TestMain {
public static void main(String[] args){
Phone phone = new Phone();
phone.charging(new VoltageAdapter());
}
}
类适配器模式注意事项和细节
1)Java是单继承机制,所以类适配器需要继承src类这一点算是一个缺点,因为这要求dst必须是接口,有一定的局限性
2)src类的方法在Adapter中都会暴露出来,也增加了使用成本
3)由于其继承了src类,所以它可以根据需求重写src类的方法,是的Adapter的灵活性增强了
2、对象适配器模式
对象适配器基本介绍
1)基本思路和类的适配器模式相同,只是将Adapter类作修改,不是继承src类,二十持有src类的实例,以解决兼容性的问题。即:持有src类,实现dst接口,完成src->dst的适配
2)根据“合成复用原则”,在系统中尽量使用关联关系(聚合)来替代继承关系
3)对象适配器模式是适配器模式常用的一种
对象适配器模式应用实例
1)应用实例说明
以生活中充电器的例子来讲解适配器,充电器本身相当于Adapter,220V交流电相当于src(即被适配者),我们的目的(dst即目标)是5V直流电,使用对象适配器模式完成
思路分析(类图):只需修改适配器即可,如下
代码实现
package designPatterns.adapter.type2;
//220v的电压
public class Voltage220 {
private Integer vol;
public Voltage220() {
this.vol = 220;
}
public Integer output200() {
System.out.println("电压220v");
return this.vol;
}
public Integer getVol() {
return vol;
}
public void setVol(Integer vol) {
this.vol = vol;
}
}
package designPatterns.adapter.type2;
//电压为5v接口
public interface IVoltage5 {
Integer output5();
}
package designPatterns.adapter.type2;
//使用聚合Voltage220的方式
public class VoltageAdapter implements IVoltage5 {
//聚合一个220v的方式
private Voltage220 voltage220;
public VoltageAdapter() {
}
public VoltageAdapter(Voltage220 voltage220) {
this.voltage220 = voltage220;
}
@Override
public Integer output5() {
int vol220 = this.voltage220.getVol();//电压为200
int vol5 = vol220 / 44;//将电压转为5V
return vol5;
}
public Voltage220 getVoltage220() {
return voltage220;
}
public void setVoltage220(Voltage220 voltage220) {
this.voltage220 = voltage220;
}
}
package designPatterns.adapter.type2;
//手机
public class Phone {
//手机充电
public void charging(IVoltage5 iVoltage5){
Integer output5 = iVoltage5.output5();
if (output5==5){
System.out.println("当前电压为5V可以充电");
}else if(output5>5){
System.out.println("当前电压大于5V,手机爆炸");
}
}
}
package designPatterns.adapter.type2;
public class TestMain {
public static void main(String[] args){
Phone phone = new Phone();
phone.charging(new VoltageAdapter());
}
}
对象适配器模式注意事项和细节
1)对象适配器和类适配器其实算是同一种思想,只不过实现方式不同。
根据合成复用原则,使用组合替代继承,所以它解决了类适配器必须继承src的局限性问题,也不再要求dst必须是接口。
2)使用成本更低,更灵活。
3、接口适配器模式
基本介绍
1)一些书籍称为:适配器模式(Default Adapter Pattern)或缺省适配器模式。
2)核心思想:当不需要全部实现接口提供的方法时,可先设计一个抽象类实现接口,并为接口中每个方法提供一个默认实现(空方法),那么该抽象类的子类可由选择地覆盖父类的某些方法来实现需求
3)适用于一个接口不想使用其所有的方法的情况。
接口适配器模式应用实例
package designPatterns.adapter.type3;
//220v的电压
public class Voltage220 {
private Integer vol;
public Voltage220() {
this.vol = 220;
}
public Integer output200() {
System.out.println("电压220v");
return this.vol;
}
public Integer getVol() {
return vol;
}
public void setVol(Integer vol) {
this.vol = vol;
}
}
package designPatterns.adapter.type3;
//电压为5v接口
public interface IVoltage5 {
Integer output5();
}
package designPatterns.adapter.type3;
//抽象的类
public abstract class VoltageAdapter implements IVoltage5 {
//空实现
@Override
public Integer output5() {
return null;
}
}
package designPatterns.adapter.type3;
//手机
public class Phone {
//手机充电
public void charging(IVoltage5 iVoltage5){
Integer output5 = iVoltage5.output5();
if (output5==5){
System.out.println("当前电压为5V可以充电");
}else if(output5>5){
System.out.println("当前电压大于5V,手机爆炸");
}
}
}
package designPatterns.adapter.type3;
public class TestMain {
public static void main(String[] args){
Phone phone = new Phone();
//使用匿名内部类的方式,重写方法
VoltageAdapter voltageAdapter = new VoltageAdapter(){
//使用内名内部类的方式
@Override
public Integer output5() {
Voltage220 voltage220 = new Voltage220();
int vol220 = voltage220.getVol();
int vol5 = vol220/44;
return vol5;
}
};
phone.charging(voltageAdapter);
}
}
适配器模式在SpringMVC框架应用的源码剖析
1)SpringMVC中的HandlerAdapter,就是用来适配器模式
2)SpringMVC处理请求的流程回顾
3)使用HandlerAdater的原因分析
可以看到处理器的类型不同,有多重实现方式,那么调用发时就不是确定的,如果需要直接调用Coontroller方法,需要调用的时候就得不断是使用if else 来进行判断是哪一种子类然后执行。那么如果后面要扩展Controller,就得修改原来的代码,这样违背了OCP原则。
4)代码分析
适配器模式的注意事项和细节
1)三种命名方式,是根据src是怎样的形式给到Adapter(在Adapter里的形式)来命名的。
2)类适配器:以类给到,在Adapter里,就是将src当作类,继承
对象适配器:以对象给到,在Adapter里,将src作为一个对象,持有
接口适配器:以接口给到,在Adapter里,将src作为一个接口,实现
3)Adapter模式最大的作用还是将原本不兼容的接口融合在一起工作。
4)实际开发中,实现起来不拘泥于我们讲解的三种经典模式