美文网首页
设计模式之适配器模式

设计模式之适配器模式

作者: Tinyspot | 来源:发表于2023-07-17 21:05 被阅读0次

1. 适配器模式(Adapter Pattern)

  • Adapter is a structural design pattern that allows objects with incompatible interfaces to collaborate.
  • Also known as: Wrapper
  • 适配器用来做适配,将不兼容的接口转换为可兼容的接口,让原本由于接口不兼容而不能一起工作的类可以一起工作

1.1 日常生活场景

  • 家用电器是交流电,而计算机工作电源是直流电,电源适配器(AC Adapter)将交流电转换为直流电
  • 注意:一个适配器只能适配一个被适配对象类型,因为适配器与被适配对象类型之间的耦合是静态关联关系(对象引用或继承)

1.2 组成部分

  • Target:目标接口
  • Adaptee:被适配者
  • Adapter:适配器,将 Adaptee 转换为 Target

2. 两种实现方式

2.1 类适配器

  • 类适配器通过继承适配者类(Adaptee)实现的
image.png
public interface Target {
    String service(String param);
}

public class Adaptee {
    public String execute(String param) {
        return "Adaptee@" + param;
    }
}
public class Adapter extends Adaptee implements Target {
    @Override
    public String service(String param) {
        String execute = super.execute(param);
        // do something
        return "do something: " + execute;
    }
}

测试代码:

@Test
public void test() {
    Target target = new Adapter();
    String test = target.service("test");
    System.out.println(test);
}

2.2 对象适配器

  • 对象适配器使用组合关系来实现,包含一个适配器者(Adaptee)的引用
image.png
public class Adapter implements Target {
    private Adaptee adaptee;

    public Adapter(Adaptee adaptee) {
        this.adaptee = adaptee;
    }

    @Override
    public String service(String param) {
        String execute = adaptee.execute(param);
        // do something
        return "do something: " + execute;
    }
}

测试代码:

@Test
public void test() {
    Target target = new Adapter(new Adaptee());
    String test = target.service("test");
    System.out.println(test);
}

3. 应用

3.1 Spring MVC中的HandlerAdapter适配器

HandlerAdapter
public class DispatcherServlet extends FrameworkServlet {
  protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {
    // ...
    ModelAndView mv = null;
    mappedHandler = getHandler(processedRequest);
    HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());
    mv = ha.handle(processedRequest, response, mappedHandler.getHandler());
    // ...
  }

  protected HandlerExecutionChain getHandler(HttpServletRequest request) throws Exception {
    // ...
  }

}

相关文章

  • 简说设计模式之适配器模式

    前言:对于设计模式基础概念可以去看[简说设计模式之设计模式概述] 一、什么是适配器模式 适配器模式(Adapter...

  • 设计模式 - 目录

    设计模式01 - 单例模式 设计模式02 - 工厂模式 设计模式03 - 建造者模式 设计模式04 - 适配器模式...

  • 设计模式详解——适配器模式

    本篇文章介绍一种设计模式——命令模式。本篇文章内容参考《JAVA与模式》之适配器模式,Android设计模式源码解...

  • 设计模式之适配器模式

    设计模式之适配器模式 1. 模式定义 适配器模式又称包装器模式,属于结构型模式,它可以将一个接口转换成客户希望的另...

  • 最常用的设计模式---适配器模式(C++实现)

    适配器模式属于结构型的设计模式,它是结构型设计模式之首(用的最多的结构型设计模式)。 适配器设计模式也并不复杂,适...

  • iOS设计模式(5)策略模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器...

  • iOS设计模式(6)模板模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器...

  • iOS设计模式(7)建造者模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器...

  • iOS设计模式(4)抽象工厂模式

    设计模式系列文章 《iOS设计模式(1)简单工厂模式》《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器...

  • iOS设计模式(1)简单工厂模式

    设计模式系列文章 《iOS设计模式(2)工厂模式》《iOS设计模式(3)适配器模式》《iOS设计模式(4)抽象工厂...

网友评论

      本文标题:设计模式之适配器模式

      本文链接:https://www.haomeiwen.com/subject/zjggudtx.html