大数据知识体系
首页
数据结构与算法
  • JVM
  • Java
  • Scala
  • Python
设计模式
  • MySQL
  • Redis
  • HDFS
  • HBase
  • ClickHouse
  • ElasticSearch
  • Iceberg
  • Hudi
  • Spark
  • Flink
  • Hive
  • Yarn
  • Zookeeper
  • Maven
  • Git
  • 数据仓库
  • 用户画像
  • 指标体系
数据治理
关于
首页
数据结构与算法
  • JVM
  • Java
  • Scala
  • Python
设计模式
  • MySQL
  • Redis
  • HDFS
  • HBase
  • ClickHouse
  • ElasticSearch
  • Iceberg
  • Hudi
  • Spark
  • Flink
  • Hive
  • Yarn
  • Zookeeper
  • Maven
  • Git
  • 数据仓库
  • 用户画像
  • 指标体系
数据治理
关于
  • 设计模式概述
  • 创建型模式

    • 单例模式
    • 简单工厂模式
    • 工厂方法模式
    • 抽象工厂模式
    • 建造者模式
    • 原型模式
  • 结构型模式

    • 适配器模式
      • 一、概述
        • 1.1 解决了什么问题
        • 1.2 解决方案
      • 二、实现方式
        • 2.1 角色
        • 2.2 代码
      • 三、源码中的应用
    • 装饰器模式
    • 代理模式
    • 外观模式
    • 桥接模式
    • 组合模式
    • 享元模式
  • 行为型模式

    • 策略模式
    • 模板方法模式
    • 观察者模式
    • 迭代器模式
    • 责任链模式
    • 命令模式
    • 备忘录模式
    • 状态模式
    • 访问者模式
    • 中介者模式
    • 解释器模式
  • 设计模式
  • 结构型模式
Will
2022-03-29
目录

适配器模式

# 一、概述

适配器(Adapter)模式将一类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作那些类可以一起工作。

# 1.1 解决了什么问题

适配器模式主要解决接口之间不兼容的问题。比如一些笔记本做的越来越轻巧,所以只保留了 Type-C 接口,为了能使 USB、HDMI 等设备同样在笔记本上正常使用,所以需要一些转换器来做适配。

# 1.2 解决方案

创建一个适配器来兼容两边的接口,将复杂的对象属性适配工作隐藏在幕后,对客户端来说察觉不到适配器的存在。

使用适配器模式的前提是代码中使用了大量的接口编程。

# 二、实现方式

# 2.1 角色

适配器模式中有三个角色:

  1. 目标角色:是指客户端要调用的接口,比如要使用的 U 盘、或 HDMI 线。
  2. 被适配角色:系统中已经存在的角色,且无法更改,比如 Mac 电脑的 Type-C 接口。
  3. 适配器:适配器模式的核心,将被适配角色的接口转换成目标角色期望的接口,对目标角色隐藏复杂的转换过程。

# 2.2 代码

被适配的角色:

/**
 * 圆孔类
 */
public class RoundHole {
    /**
     * 半径
     */
    private double radius;

    public RoundHole(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    /**
     * 检验圆钉是否适配圆孔
     * @param peg 圆钉
     * @return
     */
    public boolean fits(RoundPeg peg) {
        // 如果圆孔的半径大于等于圆钉的半径,则适配
        return (this.getRadius() >= peg.getRadius());
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

被适配角色原本期望的参数类:

/**
 * 圆钉
 */
public class RoundPeg {
    private double radius;

    public RoundPeg() {}

    public RoundPeg(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

目标角色:

/**
 * 方钉
 */
public class SquarePeg {
    /**
     * 方钉边长
     */
    private double width;

    public SquarePeg(double width) {
        this.width = width;
    }

    public double getWidth() {
        return width;
    }

    public double getSquare() {
        return Math.pow(this.width, 2);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

适配器:

/**
 * 方钉的圆孔适配器
 */
public class SquarePegAdapter extends RoundPeg {
    private SquarePeg peg;

    public SquarePegAdapter(SquarePeg peg) {
        this.peg = peg;
    }

    @Override
    public double getRadius() {
        // 计算方钉的最小圆半径(就是正方行中心点到其中一个顶点的距离),才能适配圆孔
        return (Math.sqrt(Math.pow((peg.getWidth() / 2), 2) * 2));
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

使用示例:

public class AdapterTest {
    public static void main(String[] args) {
        RoundHole hole = new RoundHole(5);
        RoundPeg roundPeg = new RoundPeg(3);

        // 圆孔半径大于等于圆钉半径,说明适配
        System.out.println(hole.fits(roundPeg));

        SquarePeg squarePeg = new SquarePeg(5);
        // 方钉直接和圆孔适配会报错
        // hole.fits(squarePeg);

        // 借助方钉适配器
        SquarePegAdapter adapter = new SquarePegAdapter(squarePeg);
        // 适配器计算得到的方钉适合的最大圆孔半径
        System.out.println(adapter.getRadius());
        // 适配
        System.out.println(hole.fits(adapter));
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# 三、源码中的应用

  1. java.util.Arrays#asList() (opens new window)
  2. java.util.Collections#list() (opens new window)
  3. java.util.Collections#enumeration() (opens new window)
  4. javax.xml.bind.annotation.adapters.XMLAdapter (opens new window)
上次更新: 2023/11/01, 03:11:44

← 原型模式 装饰器模式→

Theme by Vdoing | Copyright © 2022-2023 Will 蜀ICP备2022002285号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式