Java描述设计模式(07):适配器模式
|
本文源码:
GitHub·点这里 ||
GitEE·点这里
一、适配器模式简介1、基础概念
2、生活场景基于适配器模式,把220V的电压,转换为需要的110V电压。 public class C01_InScene {
public static void main(String[] args) {
CurrentAdapter adapter = new CurrentAdapter() ;
System.out.println(adapter.get110VCurrent()) ;
}
}
// 220V电流
class Current220V {
public int get220VCurrent (){
return 220 ;
}
}
// 110V电流接口
interface Current110V {
int get110VCurrent () ;
}
// 电流适配器
class CurrentAdapter extends Current220V implements Current110V {
// 电流转换方法
@Override
public int get110VCurrent() {
int high = get220VCurrent() ;
int low = high/2 ;
return low ;
}
}
二、类适配器1、模式简介类的适配器模式把适配的类的API转换成为目标类的API。
2、核心角色
这就是所期待得到的接口。
现在需要适配的接口。
适配器类是本模式的核心。适配器把源接口转换成目标接口。 3、源码实现interface Target {
void sampleOperation1();
void sampleOperation2();
}
class Adaptee {
public void sampleOperation1(){
System.out.println("Adaptee.sampleOperation1()");
}
}
class Adapter extends Adaptee implements Target{
@Override
public void sampleOperation2() {
System.out.println("Adapter.sampleOperation2()");
}
}
三、对象适配器1、模式简介
2、源码实现interface Target1 {
void sampleOperation1();
void sampleOperation2();
}
class Adaptee1 {
public void sampleOperation1(){
System.out.println("Adaptee.sampleOperation1()");
}
}
class Adapter1 implements Target1 {
private Adaptee1 adaptee ;
public Adapter1 (Adaptee1 adaptee){
this.adaptee = adaptee;
}
public void sampleOperation1() {
this.adaptee.sampleOperation1();
}
@Override
public void sampleOperation2() {
System.out.println("Adapter.sampleOperation2()");
}
}
四、接口适配器1、模式简介缺省(接口)适配(Default Adapter)模式为一个接口提供缺省实现,这样子类型可以从这个缺省实现进行扩展,而不必从原有接口进行扩展。
2、源代码实现public class C04_AdapterInte {
public static void main(String[] args) {
ServiceAdapter adapter = new ServiceAdapter(){
@Override
public int serviceOperation2() {
return 22 ;
}
};
System.out.println(adapter.serviceOperation2());
}
}
interface AbstractService {
void serviceOperation1();
int serviceOperation2();
String serviceOperation3();
}
class ServiceAdapter implements AbstractService{
@Override
public void serviceOperation1() {
}
@Override
public int serviceOperation2() {
return 0;
}
@Override
public String serviceOperation3() {
return null;
}
}
五、Spring框架应用1、应用场景描述在SpringMvc执行控制执行请求的时候,有这样一个流程 1)前段控制器DispatcherServlet调用处理器适配器去执行Handler(也就是Controller); 2、流程分析
Controller和HandlerAdapter两核心接口。
适配器接口,使Handler有对应的适配器实现类,适配器代替Handler(控制层Controller)执行相应的方法。 public interface HandlerAdapter {
// 判断类型是否匹配
boolean supports(Object var1);
// 执行方法,返回ModelAndView
ModelAndView handle(HttpServletRequest var1,HttpServletResponse var2,Object var3)
throws Exception;
}
supports()方法传入处理器,判断适配器是否支持,如果支持则返回支持的适配器实现类。
抽取源码中体现流程的几个步骤。 protected void doDispatch(HttpServletRequest request,HttpServletResponse response) throws Exception {
HandlerExecutionChain mappedHandler = null;
mappedHandler = this.getHandler(processedRequest);
HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());
mv = ha.handle(processedRequest,response,mappedHandler.getHandler());
mappedHandler.applyPostHandle(processedRequest,mv);
}
最后看下supports和handle两个方法的具体实现。 public class SimpleControllerHandlerAdapter implements HandlerAdapter {
public SimpleControllerHandlerAdapter() {
}
public boolean supports(Object handler) {
return handler instanceof Controller;
}
public ModelAndView handle(HttpServletRequest request,HttpServletResponse response,Object handler)
throws Exception {
return ((Controller)handler).handleRequest(request,response);
}
}
六、适配器优缺点1、优点分析
2、缺点分析
七、源代码地址GitHub·地址 https://github.com/cicadasmile/model-arithmetic-parent GitEE·地址 https://gitee.com/cicadasmile/model-arithmetic-parent
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |






