JAVA 适配器模式讲解和代码示例

简介: JAVA 适配器模式讲解和代码示例

使用示例适配器模式在 Java 代码中很常见 基于一些遗留代码的系统常常会使用该模式 在这种情况下 适配器让遗留代码与现代的类得以相互合作

Java 核心程序库中有一些标准的适配器

  • java.util.Arrays#asList()
  • java.util.Collections#list()
  • java.util.Collections#enumeration()
  • java.io.InputStreamReader(InputStream) 返回 Reader对象
  • java.io.OutputStreamWriter(OutputStream) 返回 Writer对象
  • javax.xml.bind.annotation.adapters.XmlAdapter#marshal()#unmarshal()

识别方法适配器可以通过以不同抽象或接口类型实例为参数的构造函数来识别 当适配器的任何方法被调用时 它会将参数转换为合适的格式 然后将调用定向到其封装对象中的一个或多个方法

01让方钉适配圆孔

这个简单的例子展示了适配器如何让不兼容的对象相互合作


02round

round/RoundHole.java: 圆孔

package refactoring_guru.adapter.example.round;/**
* RoundHoles are compatible with RoundPegs.
*/public class RoundHole {    private double radius;    public RoundHole(double radius) {        this.radius = radius;
    }    public double getRadius() {        return radius;
    }    public boolean fits(RoundPeg peg) {        boolean result;        result = (this.getRadius() >= peg.getRadius());        return result;
    }
}

round/RoundPeg.java: 圆钉

package refactoring_guru.adapter.example.round;/**
* RoundPegs are compatible with RoundHoles.
*/public class RoundPeg {    private double radius;    public RoundPeg() {}    public RoundPeg(double radius) {        this.radius = radius;
    }    public double getRadius() {        return radius;
    }
}

03square

square/SquarePeg.java: 方钉

package refactoring_guru.adapter.example.square;/**
* SquarePegs are not compatible with RoundHoles (they were implemented by
* previous development team). But we have to integrate them into our program.
*/public class SquarePeg {    private double width;    public SquarePeg(double width) {        this.width = width;
    }    public double getWidth() {        return width;
    }    public double getSquare() {        double result;        result = Math.pow(this.width, 2);        return result;
    }
}

04adapters

adapters/SquarePegAdapter.java: 方钉到圆孔的适配器

package refactoring_guru.adapter.example.adapters;import refactoring_guru.adapter.example.round.RoundPeg;import refactoring_guru.adapter.example.square.SquarePeg;/**
* Adapter allows fitting square pegs into round holes.
*/public class SquarePegAdapter extends RoundPeg {    private SquarePeg peg;    public SquarePegAdapter(SquarePeg peg) {        this.peg = peg;
    }    @Override
    public double getRadius() {        double result;        // Calculate a minimum circle radius, which can fit this peg.
        result = (Math.sqrt(Math.pow((peg.getWidth() / 2), 2) * 2));        return result;
    }
}

Demo.java: 客户端代码

package refactoring_guru.adapter.example;import refactoring_guru.adapter.example.adapters.SquarePegAdapter;import refactoring_guru.adapter.example.round.RoundHole;import refactoring_guru.adapter.example.round.RoundPeg;import refactoring_guru.adapter.example.square.SquarePeg;/**
* Somewhere in client code...
*/public class Demo {    public static void main(String[] args) {        // Round fits round, no surprise.
        RoundHole hole = new RoundHole(5);        RoundPeg rpeg = new RoundPeg(5);        if (hole.fits(rpeg)) {            System.out.println("Round peg r5 fits round hole r5.");
        }        SquarePeg smallSqPeg = new SquarePeg(2);        SquarePeg largeSqPeg = new SquarePeg(20);        // hole.fits(smallSqPeg); // Won't compile.
        // Adapter solves the problem.
        SquarePegAdapter smallSqPegAdapter = new SquarePegAdapter(smallSqPeg);        SquarePegAdapter largeSqPegAdapter = new SquarePegAdapter(largeSqPeg);        if (hole.fits(smallSqPegAdapter)) {            System.out.println("Square peg w2 fits round hole r5.");
        }        if (!hole.fits(largeSqPegAdapter)) {            System.out.println("Square peg w20 does not fit into round hole r5.");
        }
    }
}

OutputDemo.txt: 执行结果

Round peg r5 fits round hole r5.
Square peg w2 fits round hole r5.
Square peg w20 does not fit into round hole r5.


以上是23种设计模式漫画版系列—适配器模式,全部内容了。

相关文章
|
5天前
|
Java 索引
String字符串常用函数以及示例 JAVA基础
String字符串常用函数以及示例 JAVA基础
|
6天前
|
Java 程序员 图形学
程序员教你用代码制作飞翔的小鸟--Java小游戏,正好拿去和给女神一起玩
《飞扬的小鸟》Java实现摘要:使用IntelliJ IDEA和JDK 16开发,包含小鸟类`Bird`,处理小鸟的位置、速度和碰撞检测。代码示例展示小鸟图像的加载、绘制与旋转。同时有`Music`类用于循环播放背景音乐。游戏运行时检查小鸟是否撞到地面、柱子或星星,并实现翅膀煽动效果。简单易懂,可直接复制使用。
|
2天前
|
Java 测试技术
如何提高Java代码的可读性
Java是一种常用的编程语言,但是写出易懂且可读性高的代码却是一项挑战。本文将分享一些技巧和建议,帮助您提高Java代码的可读性和可维护性。
|
5天前
|
Java Kotlin
java调用kotlin代码编译报错“找不到符号”的问题
java调用kotlin代码编译报错“找不到符号”的问题
17 10
|
6天前
|
前端开发 Java Spring
Java Web ——MVC基础框架讲解及代码演示(下)
Java Web ——MVC基础框架讲解及代码演示
13 1
|
6天前
|
设计模式 前端开发 网络协议
Java Web ——MVC基础框架讲解及代码演示(上)
Java Web ——MVC基础框架讲解及代码演示
8 0
|
6天前
|
Java
Java的取余如何编写代码
【5月更文挑战第9天】Java的取余如何编写代码
22 5
|
6天前
|
Java
代码实例演示Java字符串与输入流互转
代码实例演示Java字符串与输入流互转
10 1
|
6天前
|
存储 安全 Java
掌握8条泛型规则,打造优雅通用的Java代码
掌握8条泛型规则,打造优雅通用的Java代码
掌握8条泛型规则,打造优雅通用的Java代码
|
6天前
|
数据库连接
java+ssm+vue代码视频学习讲解
java+ssm+vue代码视频学习讲解
11 0