类适配器
假设需要一个220v转5v的适配器给手机充电
需要适配的是220v电压(Source),需要提供的可使用接口为5v(Target),通过继承方法获得父类的相应方法则为类适配器
public class Source {
public int output220V(){
return 220;
}
}
public interface Target {
public int output();
}
public class Adapter extends Source implements Target{
@Override
public int output() {
return super.output220V()/44;
}
}
使用的客户(phone需要5v电压)
public class Phone {
public void charging(Target target){
if(target.output() == 5){
System.out.println("电压正确,开始充电......");
}else {
System.out.println("手机炸了!");
}
}
}
public class Client {
private Phone phone;
public Phone getPhone() {
return phone;
}
public void setPhone(Phone phone) {
this.phone = phone;
}
public void chargingPhone(Adapter adapter){
phone.charging(adapter);
}
}
测试类
public class Test {
public static void main(String[] args) {
Client client = new Client();
client.setPhone(new Phone());
/**
* java单继承机制导致要求Target必须为接口,有局限性
* Source的方法会暴露出来,增加使用成本
* 由于继承Source可以重写其方法,灵活性高
*/
client.chargingPhone(new Adapter());
}
}
结果
对象适配器
将继承方法改为依赖,降低了不良耦合,解决父类方法暴露问题,符合里氏代换原则
public class ObjectAdapter implements Target {
Source source;
public ObjectAdapter(Source source){
this.source = source;
}
@Override
public int output() {
return source.output220V()/44;
}
}
修改client
public void chargingPhone(ObjectAdapter adapter){
phone.charging(adapter);
}
测试类
public class Test {
public static void main(String[] args) {
Client client = new Client();
client.setPhone(new Phone());
/**
* java单继承机制导致要求Target必须为接口,有局限性
* Source的方法会暴露出来,增加使用成本
* 由于继承Source可以重写其方法,灵活性高
*/
//client.chargingPhone(new Adapter());
client.chargingPhone(new ObjectAdapter(new Source()));
}
}
结果
接口适配器
对于不需要全部实现的接口方法有选择的实现,个人认为是对于接口隔离原则的修补
public interface Source {
void m1();
void m2();
void m3();
void m4();
void m5();
}
/**
* 抽象类全部默认实现,子类通过重写选择实现想要的方法
*/
public abstract class Target implements Source{
@Override
public void m1(){
};
@Override
public void m2(){
};
@Override
public void m3(){
};
@Override
public void m4(){
};
@Override
public void m5(){
};
}
测试类
public class Test {
public static void main(String[] args) {
Target target = new Target() {
@Override
public void m1() {
System.out.println("我只实现了方法1");
}
};
target.m1();
target.m2();
}
}
结果
适配器模式相关源码——DispatcherServlet的doDispatcher
protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
...... ......代码省略
...... ......代码省略
try {
try {
ModelAndView mv = null;
Object dispatchException = null;
try {
...... ......代码省略
...... ......代码省略
// 此处通过handler执行链中的handler获取相应的处理器适配器
HandlerAdapter ha = this.getHandlerAdapter(mappedHandler.getHandler());
...... ......代码省略
...... ......代码省略
// 获取结果返回相应的ModleAndView
mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
if (asyncManager.isConcurrentHandlingStarted()) {
return;
}
this.applyDefaultViewName(processedRequest, mv);
mappedHandler.applyPostHandle(processedRequest, response, mv);
}
...... ......代码省略
...... ......代码省略
}
}
protected HandlerAdapter getHandlerAdapter(Object handler) throws ServletException {
if (this.handlerAdapters != null) {
Iterator var2 = this.handlerAdapters.iterator();
while(var2.hasNext()) {
HandlerAdapter ha = (HandlerAdapter)var2.next();
if (this.logger.isTraceEnabled()) {
this.logger.trace("Testing handler adapter [" + ha + "]");
}
if (ha.supports(handler)) {
return ha;
}
}
}
throw new ServletException("No adapter for handler [" + handler + "]: The DispatcherServlet configuration needs to include a HandlerAdapter that supports this handler");
}
/**
* 类图中的Target接口
**/
public interface HandlerAdapter {
boolean supports(Object var1);
// 目标方法handle
@Nullable
ModelAndView handle(HttpServletRequest var1, HttpServletResponse var2, Object var3) throws Exception;
long getLastModified(HttpServletRequest var1, Object var2);
}
/**
* 具体Adapter的实现,此处为AbstractHandlerMethodAdapter的实现
**/
@Nullable
public final ModelAndView handle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return this.handleInternal(request, response, (HandlerMethod)handler);
}