0
点赞
收藏
分享

微信扫一扫

设计模式(13) --适配器模式(3种实现方式)


文章目录

  • ​​基本介绍​​
  • ​​适配器模式工作原理​​
  • ​​三种实现方式​​
  • ​​类适配器模式​​
  • ​​注意:​​
  • ​​对象适配器​​
  • ​​总结​​
  • ​​接口适配器​​
  • ​​适配器模式在SpringMVC 框架应用的源码剖析​​
  • ​​总结:​​

基本介绍

设计模式(13) --适配器模式(3种实现方式)_适配器模式

适配器模式工作原理

设计模式(13) --适配器模式(3种实现方式)_mvc_02

三种实现方式

类适配器模式

设计模式(13) --适配器模式(3种实现方式)_适配器模式_03


设计模式(13) --适配器模式(3种实现方式)_适配器模式_04

代码:

设计模式(13) --适配器模式(3种实现方式)_适配器模式_05


Voltage220V

//被适配的类
public class Voltage220V {
//输出220V的电压
public int output220V() {
int src = 220;
System.out.println("电压=" + src + "伏");
return src;
}
}

IVoltage5V

//适配接口
public interface IVoltage5V {
public int output5V();
}

VoltageAdapter

//适配器类
public class VoltageAdapter extends Voltage220V implements IVoltage5V {

@Override
public int output5V() {
// TODO Auto-generated method stub
//获取到220V电压
int srcV = output220V();
int dstV = srcV / 44 ; //转成 5v
return dstV;
}
}

测试:Client

public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(" === 类适配器模式 ====");
Phone phone = new Phone();
phone.charging(new VoltageAdapter());
}
}

设计模式(13) --适配器模式(3种实现方式)_spring_06

注意:

设计模式(13) --适配器模式(3种实现方式)_spring_07

对象适配器

设计模式(13) --适配器模式(3种实现方式)_适配器模式_08


设计模式(13) --适配器模式(3种实现方式)_spring_09


类图

设计模式(13) --适配器模式(3种实现方式)_mvc_10


代码:

设计模式(13) --适配器模式(3种实现方式)_mvc_11


其他类不变,改下适配器类VoltageAdapter:

//适配器类
public class VoltageAdapter implements IVoltage5V {
private Voltage220V voltage220V; // 关联关系-聚合

//通过构造器,传入一个 Voltage220V 实例
public VoltageAdapter(Voltage220V voltage220v) {
this.voltage220V = voltage220v;
}

@Override
public int output5V() {
int dst = 0;
if(null != voltage220V) {
int src = voltage220V.output220V();//获取220V 电压
System.out.println("使用对象适配器,进行适配~~");
dst = src / 44;
System.out.println("适配完成,输出的电压为=" + dst);
}
return dst;
}
}

测试Client

public class Client {

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(" === 对象适配器模式 ====");
Phone phone = new Phone();
phone.charging(new VoltageAdapter(new Voltage220V()));
}

}

设计模式(13) --适配器模式(3种实现方式)_spring_12

总结

设计模式(13) --适配器模式(3种实现方式)_mvc_13

接口适配器

设计模式(13) --适配器模式(3种实现方式)_spring_14


安卓上使用接口适配器的实例:

设计模式(13) --适配器模式(3种实现方式)_适配器模式_15


设计模式(13) --适配器模式(3种实现方式)_适配器模式_16


设计模式(13) --适配器模式(3种实现方式)_mvc_17


自己写个案例:

设计模式(13) --适配器模式(3种实现方式)_mvc_18


设计模式(13) --适配器模式(3种实现方式)_mvc_19


Interface4

public interface Interface4 {
public void m1();
public void m2();
public void m3();
public void m4();
}

AbsAdapter

//在AbsAdapter 我们将 Interface4 的方法进行默认实现
public abstract class AbsAdapter implements Interface4 {

//默认实现
public void m1() {

}

public void m2() {

}

public void m3() {

}

public void m4() {

}
}

测试Client

public class Client {
public static void main(String[] args) {

AbsAdapter absAdapter = new AbsAdapter() {
//只需要去覆盖我们 需要使用 接口方法
@Override
public void m1() {
// TODO Auto-generated method stub
System.out.println("使用了m1的方法");
}
};

absAdapter.m1();
}
}

设计模式(13) --适配器模式(3种实现方式)_适配器模式_20

适配器模式在SpringMVC 框架应用的源码剖析

设计模式(13) --适配器模式(3种实现方式)_spring_21


设计模式(13) --适配器模式(3种实现方式)_spring_22


动手写 SpringMVC 通 过 适配器设计模式 获取到对应的 Controller 的源码

设计模式(13) --适配器模式(3种实现方式)_适配器模式_23


Controller

//多种Controller实现  
public interface Controller {

}

class HttpController implements Controller {
public void doHttpHandler() {
System.out.println("http...");
}
}

class SimpleController implements Controller {
public void doSimplerHandler() {
System.out.println("simple...");
}
}

class AnnotationController implements Controller {
public void doAnnotationHandler() {
System.out.println("annotation...");
}
}

HandlerAdapter

///定义一个Adapter接口 
public interface HandlerAdapter {
public boolean supports(Object handler);

public void handle(Object handler);
}

// 多种适配器类

class SimpleHandlerAdapter implements HandlerAdapter {

public void handle(Object handler) {
((SimpleController) handler).doSimplerHandler();
}

public boolean supports(Object handler) {
return (handler instanceof SimpleController);
}

}

class HttpHandlerAdapter implements HandlerAdapter {

public void handle(Object handler) {
((HttpController) handler).doHttpHandler();
}

public boolean supports(Object handler) {
return (handler instanceof HttpController);
}

}

class AnnotationHandlerAdapter implements HandlerAdapter {

public void handle(Object handler) {
((AnnotationController) handler).doAnnotationHandler();
}

public boolean supports(Object handler) {

return (handler instanceof AnnotationController);
}

}

DispatchServlet

import java.util.ArrayList;
import java.util.List;
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() {

// 此处模拟SpringMVC从request取handler的对象,
// 适配器可以获取到希望的Controller
HttpController controller = new HttpController();
// AnnotationController controller = new AnnotationController();
//SimpleController controller = new SimpleController();
// 得到对应适配器
HandlerAdapter adapter = getHandler(controller);
// 通过适配器执行对应的controller对应方法
adapter.handle(controller);
}
public HandlerAdapter getHandler(Controller controller) {
//遍历:根据得到的controller(handler), 返回对应适配器
for (HandlerAdapter adapter : this.handlerAdapters) {
if (adapter.supports(controller)) {
return adapter;
}
}
return null;
}
public static void main(String[] args) {
new DispatchServlet().doDispatch(); // http...
}
}

设计模式(13) --适配器模式(3种实现方式)_mvc_24


设计模式(13) --适配器模式(3种实现方式)_spring_25


个人感觉:这个写法太精妙了,因为各个controller方法名不同。①怎么让这些方法变成一个统一的? 于是想到用每个controller对应一个adapter实现类,这个adapter统一实现adapet接口。②如何判断传进来的controller用哪个adapt实现类?这句话白话说就是 根据controller找到对应的adapt,于是在dispatchservelt中先把所有的adapt实现来放在一个集合中,遍历,用每个adapt提供的support方法,找到自己。于是 就是可以使用这个对应的adapter干自己喜欢的事情了。!!

总结:

设计模式(13) --适配器模式(3种实现方式)_mvc_26


举报

相关推荐

0 条评论