• 设计模式之适配器模式(五)


    目录

    1. 背景

    1.1 问题描述

    1.2 适配器模式

    2. 适配器

    2.1 类适配器

    2.2 对象适配器

    2.3 接口适配器(缺省适配器/适配器)

    3. 适配器模式在SpringMVC中的应用


    1. 背景

    1.1 问题描述

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

    1.2 适配器模式

    • 适配器模式(Adapter Pattern)将某个类的接口转换成客户端期望的另一个接口表示,主的目的是兼容性,让原本因接口不匹配不能一起工作的两个类可以协同 工作。其别名为包装器(Wrapper)。
    • 主要分为三类:类适配器模式、对象适配器模式、接口适配器模式。
    • 用来扩展代码兼容性。

    2. 适配器

    2.1 类适配器

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

    1. //被适配的类
    2. public class Voltage220V {
    3. //输出220V的电压
    4. public int output220V() {
    5. int src = 220;
    6. System.out.println("电压=" + src + "伏");
    7. return src;
    8. }
    9. }
    1. //适配接口
    2. public interface IVoltage5V {
    3. public int output5V();
    4. }
    1. //适配器类
    2. public class VoltageAdapter extends Voltage220V implements IVoltage5V {
    3. @Override
    4. public int output5V() {
    5. // TODO Auto-generated method stub
    6. //获取到220V电压
    7. int srcV = output220V();
    8. int dstV = srcV / 44 ; //转成 5v
    9. return dstV;
    10. }
    11. }

    2.2 对象适配器

     持有 src类,实现 dst 类接口, 完成src->dst的适配

    根据合成复用法则,用关联关系来代替继承。是适配器模式常用的一种 

    src类和dst类没改变,只变了adapter类

    1. //适配器类
    2. public class VoltageAdapter implements IVoltage5V {
    3. private Voltage220V voltage220V; // 关联关系-聚合
    4. //通过构造器,传入一个 Voltage220V 实例
    5. public VoltageAdapter(Voltage220V voltage220v) {
    6. this.voltage220V = voltage220v;
    7. }
    8. @Override
    9. public int output5V() {
    10. int dst = 0;
    11. if(null != voltage220V) {
    12. int src = voltage220V.output220V();//获取220V 电压
    13. System.out.println("使用对象适配器,进行适配~~");
    14. dst = src / 44;
    15. System.out.println("适配完成,输出的电压为=" + dst);
    16. }
    17. return dst;
    18. }
    19. }

    2.3 接口适配器(缺省适配器/适配器)

    当不需要全部实现接口提供的方法时,可先设计一个抽象类实现接口,并为该接口中每个方法提供一个默认实现(空方法),那么该抽象类的子类可有选择地覆盖父类的某些方法来实现需求。
    1. public interface Interface4 {
    2. public void m1();
    3. public void m2();
    4. public void m3();
    5. public void m4();
    6. }
    1. //在AbsAdapter 我们将 Interface4 的方法进行默认实现
    2. public abstract class AbsAdapter implements Interface4 {
    3. //默认实现
    4. public void m1() {
    5. }
    6. public void m2() {
    7. }
    8. public void m3() {
    9. }
    10. public void m4() {
    11. }
    12. }
    1. public class Client {
    2. public static void main(String[] args) {
    3. AbsAdapter absAdapter = new AbsAdapter() {
    4. //只需要去覆盖我们 需要使用 接口方法
    5. @Override
    6. public void m1() {
    7. // TODO Auto-generated method stub
    8. System.out.println("使用了m1的方法");
    9. }
    10. };
    11. absAdapter.m1();
    12. }
    13. }

    3. 适配器模式在SpringMVC中的应用

     HandlerAdapter字面上的意思就是处理适配器,它的作用用一句话概括就是调用具体的方法对用户发来的请求来进行处理。当handlerMapping获取到执行请求的controller时,DispatcherServlet会根据controller对应的controller类型来调用相应的HandlerAdapter来进行处理。

    1. //多种Controller实现
    2. public interface Controller {
    3. }
    4. class HttpController implements Controller {
    5. public void doHttpHandler() {
    6. System.out.println("http...");
    7. }
    8. }
    9. class SimpleController implements Controller {
    10. public void doSimplerHandler() {
    11. System.out.println("simple...");
    12. }
    13. }
    14. class AnnotationController implements Controller {
    15. public void doAnnotationHandler() {
    16. System.out.println("annotation...");
    17. }
    18. }
    1. ///定义一个Adapter接口
    2. public interface HandlerAdapter {
    3. public boolean supports(Object handler);
    4. public void handle(Object handler);
    5. }
    6. // 多种适配器类
    7. class SimpleHandlerAdapter implements HandlerAdapter {
    8. public void handle(Object handler) {
    9. ((SimpleController) handler).doSimplerHandler();
    10. }
    11. public boolean supports(Object handler) {
    12. return (handler instanceof SimpleController);
    13. }
    14. }
    15. class HttpHandlerAdapter implements HandlerAdapter {
    16. public void handle(Object handler) {
    17. ((HttpController) handler).doHttpHandler();
    18. }
    19. public boolean supports(Object handler) {
    20. return (handler instanceof HttpController);
    21. }
    22. }
    23. class AnnotationHandlerAdapter implements HandlerAdapter {
    24. public void handle(Object handler) {
    25. ((AnnotationController) handler).doAnnotationHandler();
    26. }
    27. public boolean supports(Object handler) {
    28. return (handler instanceof AnnotationController);
    29. }
    30. }
    1. public class DispatchServlet {
    2. public static List handlerAdapters = new ArrayList();
    3. public DispatchServlet() {
    4. handlerAdapters.add(new AnnotationHandlerAdapter());
    5. handlerAdapters.add(new HttpHandlerAdapter());
    6. handlerAdapters.add(new SimpleHandlerAdapter());
    7. }
    8. public void doDispatch() {
    9. // 此处模拟SpringMVC从request取handler的对象,
    10. // 适配器可以获取到希望的Controller
    11. HttpController controller = new HttpController();
    12. // AnnotationController controller = new AnnotationController();
    13. //SimpleController controller = new SimpleController();
    14. // 得到对应适配器
    15. HandlerAdapter adapter = getHandler(controller);
    16. // 通过适配器执行对应的controller对应方法
    17. adapter.handle(controller);
    18. }
    19. public HandlerAdapter getHandler(Controller controller) {
    20. //遍历:根据得到的controller(handler), 返回对应适配器
    21. for (HandlerAdapter adapter : this.handlerAdapters) {
    22. if (adapter.supports(controller)) {
    23. return adapter;
    24. }
    25. }
    26. return null;
    27. }
    28. public static void main(String[] args) {
    29. new DispatchServlet().doDispatch(); // http...
    30. }
    31. }

     

     

     

  • 相关阅读:
    电影《前任4:英年早婚》观后感
    FLUX.1 实测,堪比 Midjourney 的开源 AI 绘画模型,无需本地显卡,带你免费实战
    ROS 导航
    Swift 中的并发:Continuations
    Anaconda虚拟环境配置Python库与Spyder编译器
    Nautilus Chain上线主网,为DeFi和流支付的未来构建基础
    Redis事务操作
    位运算及其使用技巧
    k8s--基础--24.3--efk--安装efk组件
    超级简单学习Shiro会话管理
  • 原文地址:https://blog.csdn.net/weixin_45734473/article/details/127670133