目录
最常见的插座转换,耳机接口转换。

Adapter类,通过继承 src类,实现 dst 类接口,完成src->dst的适配

- //被适配的类
- public class Voltage220V {
- //输出220V的电压
- public int output220V() {
- int src = 220;
- System.out.println("电压=" + src + "伏");
- return src;
- }
- }
- //适配接口
- public interface IVoltage5V {
- public int output5V();
- }
- //适配器类
- 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;
- }
-
- }
持有 src类,实现 dst 类接口, 完成src->dst的适配
根据合成复用法则,用关联关系来代替继承。是适配器模式常用的一种

src类和dst类没改变,只变了adapter类
- //适配器类
- 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;
- }
- }
- public interface Interface4 {
- public void m1();
- public void m2();
- public void m3();
- public void m4();
- }
- //在AbsAdapter 我们将 Interface4 的方法进行默认实现
- public abstract class AbsAdapter implements Interface4 {
-
- //默认实现
- public void m1() {
-
- }
-
- public void m2() {
-
- }
-
- public void m3() {
-
- }
-
- public void m4() {
-
- }
- }
- 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();
- }
- }
HandlerAdapter字面上的意思就是处理适配器,它的作用用一句话概括就是调用具体的方法对用户发来的请求来进行处理。当handlerMapping获取到执行请求的controller时,DispatcherServlet会根据controller对应的controller类型来调用相应的HandlerAdapter来进行处理。

- //多种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...");
- }
- }
- ///定义一个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);
- }
- }
- public class DispatchServlet {
-
- public static List
handlerAdapters = new ArrayList(); -
- 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...
- }
-
- }