适配器模式
1.简介
适配器模式(Adapter Pattern)将某个类的接口转换成客户端期望的另一个接口表示,主的目的是兼容性,因接口不匹配不能一起工作的两个类可以协同工作。其别名为包装器(Wrapper)
适配器模式属于结构型模式
主要分为三类:类适配器模式、对象适配器模式、接口适配器模式
工作原理
- 适配器模式:将一个类的接口转换成另一种接口.让原本接口不兼容的类可以兼容
- 从用户的角度看不到被适配者,是解耦的
- 用户调用适配器转化出来的目标接口方法,适配器再调用被适配者的相关接口方法
- 用户收到反馈结果,感觉只是和目标接口交互
2.类适配器模式
基本介绍: Adapter类,通过继承src类,实现dst类接口,完成src->dst的适配。
类适配器模式应用实例
以生活中充电器的例子来讲解适配器,充电器本身相当于Adapter,220V交流电相当于src(即被适配者)的目dst(即目标)是5V直流电
1 2 3 4 5 6 7 8 9
| public class Voltage220V { public int output220V(){ int src = 220; System.out.println("电压="+src+"V"); return 220; } }
|
1 2 3 4
| public interface IVoltage5V { public int output5V(); }
|
1 2 3 4 5 6 7 8 9 10
| public class VoltageAdapter extends Voltage220V implements IVoltage5V{
@Override public int output5V() { int src = output220V(); int dstV = src /44; return dstV; } }
|
1 2 3 4 5 6 7 8 9 10
| public class Phone { public void charging(IVoltage5V iVoltage5V){ if (iVoltage5V.output5V() == 5){ System.out.println("电压为5V,可以充电~~"); }else if (iVoltage5V.output5V() > 5){ System.out.println("电压大于5V,不可以充电~~"); } } }
|
1 2 3 4 5 6 7 8 9
| public class Client {
public static void main(String[] args) { Phone phone = new Phone(); phone.charging(new VoltageAdapter()); } }
|
注意
- Java是单继承机制,所以类适配器需要继承src类这一点算是一个缺点,因为这要求dst必须是接口,有限性;
- src类的方法在 Adapter 中都会暴露出来,也增加了使用的成本。
- 由于其继承了src类,所以它可以根据需求重写src类的方法,使得Adapter的灵活性增强了。
3.对象适配器
- 基本思路和类的适配器模式相同,只是将Adapter类作修改,不是继承src类,而是持有src类的实例.兼容性的问题。即:持有src类,实现 dst类接口,完成src->dst 的适配
- 根据“合成复用原则”,在系统中尽量使用关联关系(聚合)来替代继承关系。
- 对象适配器模式是适配器模式常用的一种
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class VoltageAdapter implements IVoltage5V {
private Voltage220V voltage220V;
public VoltageAdapter(Voltage220V voltage220V) { this.voltage220V = voltage220V; }
@Override public int output5V() { int dst = 0; if (null != voltage220V){ int src = voltage220V.output220V(); System.out.println("使用对象适配器进行转换"); dst = src / 44; System.out.println("处理完成,输出电压为="+dst); } return dst; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class Client {
public static void main(String[] args) { Phone phone = new Phone(); phone.charging(new VoltageAdapter(new Voltage220V())); } }
|
4.接口适配器模式
- 一些书籍称为:适配器模式(Default Adapter Pattern)或缺省适配器模式。
- 核心思路:当不需要全部实现接口提供的方法时,可先设计一个抽象类实现接口,并为该接口中每个方法提供一个默认实现(空方法),那么该抽象类的子类可有选择地覆盖父类的某些方法来实现需求
- 适用于一个接口不想使用其所有的方法的情况。
接口适配器模式应用实例
1 2 3 4 5 6
| public interface Interface4 { public void m1(); public void m2(); public void m3(); public void m4(); }
|
1 2 3 4 5 6 7
| public abstract class AbsAdapter implements Interface4{ public void m1(){} public void m2(){} public void m3(){} public void m4(){} }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class Client {
public static void main(String[] args) { AbsAdapter absAdapter = new AbsAdapter() { @Override public void m1() { System.out.println("使用了m1的方法"); } }; absAdapter.m1(); } }
|
5.适配器在SpringMVC中的使用
- SpringMvc中的HandlerAdapter,就使用了适配器模式
- SpringMVC处理请求的流程回顾
- 使用HandlerAdapter的原因分析:
可以看到处理器的类型不同,有多重实现方式,那么调用方式就不是确定的,如果需要直接调用Controller方法,需要调用的时候就得不断是使用if else来进行判断是哪一种子类然后执行。那么如果后面要扩展Controller,就得修改原来的代码,这样违背了OCP原则。
4)代码分析+Debug源码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public interface Controller { }
class HttpController implements Controller{ public void doHttpHandler(){ System.out.println("http..."); } } class SimpleController implements Controller{ public void doSimpleHandler(){ System.out.println("simple..."); } } class AnnotationController implements Controller{ public void doAnnotationHandler(){ System.out.println("annotation..."); } }
|
适配器
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
| public interface HandlerAdapter { public boolean supports(Object handler); public void handler(Object handler); }
class SimpleHandlerAdapter implements HandlerAdapter{
@Override public boolean supports(Object handler) { return handler instanceof SimpleController; }
@Override public void handler(Object handler) { ((SimpleController)handler).doSimpleHandler(); } } class HttpHandlerAdapter implements HandlerAdapter{
@Override public boolean supports(Object handler) { return handler instanceof HttpController; }
@Override public void handler(Object handler) { ((HttpController)handler).doHttpHandler(); } } class AnnotationHandlerAdapter implements HandlerAdapter{
@Override public boolean supports(Object handler) { return handler instanceof AnnotationController; }
@Override public void handler(Object handler) { ((AnnotationController)handler).doAnnotationHandler(); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| public class DispatchServlet {
public static List<HandlerAdapter> handlerAdapters = new ArrayList<HandlerAdapter>();
public DispatchServlet(){ handlerAdapters.add(new AnnotationHandlerAdapter()); handlerAdapters.add(new HttpHandlerAdapter()); handlerAdapters.add(new SimpleHandlerAdapter()); } public void doDispatch(){ AnnotationController controller = new AnnotationController(); HandlerAdapter adapter = getHandler(controller); adapter.handler(controller); }
public HandlerAdapter getHandler(Controller controller){ for (HandlerAdapter adapter : this.handlerAdapters){ if (adapter.supports(controller)){ return adapter; } } return null; }
public static void main(String[] args) { new DispatchServlet().doDispatch(); } }
|
Spring定义了一个适配接口,使得每一种Controller有一种对应的适配器实现类适配器代替controller执行相应的方法
扩展Controller时,只需要增加一个适配器类就完成了SpringMvC的扩展了,这就是设计模式的力量
6.注意事项
- 三种命名方式,是根据 src是以怎样的形式给到Adapter(在 Adapter 里的形式)来命名的。
- 类适配器:以类给到,在Adapter 里,就是将src当做类,继承
对象适配器:以对象给到,在 Adapter里,将src作为一个对象,持有
接口适配器:以接口给到,在Adapter里,将src作为一个接口,实现
- Adapter模式最大的作用还是将原本不兼容的接口融合在一起工作。
- 实际开发中,实现起来不拘泥于我们讲解的三种经典形式