博客
关于我
设计模式6-适配器模式
阅读量:716 次
发布时间:2019-03-21

本文共 2703 字,大约阅读时间需要 9 分钟。

适配器模式详解

适配器模式是一种常见的软件设计模式,旨在解决接口不匹配或功能需求改变的问题。以下将从类适配器模式、对象适配器模式以及接口适配器模式三个方面详细阐述。

类适配器模式

类适配器模式的核心思想是:有一种源类(Source),其中含有一个需要适配的方法;目标是定义一个接口(Targetable),希望将源类的功能扩展到目标接口中。这时,通过创建一个Adapter类,使源类的方法能够实现目标接口的要求。

源代码示例

public class Source {    public void method1() {        System.out.println("这是一段源代码");    }}

目标接口

public interface Targetable {    void method1();    void method2();}

适配器实现

public class Adapter implements Targetable {    @Override    public void method2() {        System.out.println("这是一段适配器新增的功能");    }    @Override    public void method1() {        // 调用源类的方法        ((Source) this).method1();    }}

使用示例

public class AdapterTest {    public static void main(String[] args) {        Targetable target = new Adapter();        target.method1();        target.method2();    }}

通过上述代码,我们可以看见Adapter类不仅实现了Targetable接口的method2()方法,还重写了method1(),并通过动态_cast调用源类的方法,从而完成了对源接口方法的适配。

对象适配器模式

对象适配器模式与类适配器模式的核心思想相似,但实现方式有所不同。对象适配器并不直接继承源类,而是持有源类的实例,并通过调用实例方法来实现目标接口的需求。

适配器实现

public class Wrapper implements Targetable {    private Source source;        public Wrapper(Source source) {        this.source = source;    }        @Override    public void method2() {        System.out.println("这是一段适配器新增的功能");    }        @Override    public void method1() {        source.method1();    }}

使用示例

public class AdapterTest {    public static void main(String[] args) {        Source source = new Source();        Targetable target = new Wrapper(source);        target.method1();        target.method2();    }}

相比类适配器,对象适配器的主要优势在于减少了类间直接继承的复杂性,而是通过持有实例的方式来实现功能适配。

接口适配器模式

接口适配器模式的主要场景是当我们需要实现一个接口,但其中某些方法不需要完全实现时。通过接口适配器,我们可以创建一个基准实现,其他具体实现类只需要继承基准实现即可。

基准接口

public interface Sourceable {    void method1();    void method2();}

基准实现

public abstract class Wrapper2 implements Sourceable {    @Override    public void method1() {}    @Override    public void method2() {}}

具体实现类

public class SourceSub1 extends Wrapper2 {    @Override    public void method1() {        System.out.println("SourceSub1实现了method1");    }}
public class SourceSub2 extends Wrapper2 {    @Override    public void method2() {        System.out.println("SourceSub2实现了method2");    }}

使用示例

public class WrapperTest {    public static void main(String[] args) {        Sourceable sourceable1 = new SourceSub1();        Sourceable sourceable2 = new SourceSub2();        sourceable1.method1();        sourceable1.method2();        sourceable2.method1();        sourceable2.method2();    }}

通过接口适配器模式,我们可以灵活地实现接口中的不同方法,减少了不必要的代码重复。

总结

  • 类适配器模式:适用于需要将源类的功能适配到新接口时,通过创建一个继承源类的新类来实现目标接口的要求。
  • 对象适配器模式:适用于将源类的实例持有,并通过调用实例方法来实现目标接口的需求。
  • 接口适配器模式:适用于需要对接口进行部分实现时,通过创建一个基准实现类,其他实现类只需继承基准实现即可。

这三种适配器模式各有特点,但都在解决接口兼容性问题方面发挥着重要作用。选择适当的适配器模式,是成功设计的关键所在。

转载地址:http://bbvrz.baihongyu.com/

你可能感兴趣的文章
MySQL 8.0 恢复孤立文件每表ibd文件
查看>>
MySQL 8.0开始Group by不再排序
查看>>
mysql ansi nulls_SET ANSI_NULLS ON SET QUOTED_IDENTIFIER ON 什么意思
查看>>
multi swiper bug solution
查看>>
MySQL Binlog 日志监听与 Spring 集成实战
查看>>
MySQL binlog三种模式
查看>>
multi-angle cosine and sines
查看>>
Mysql Can't connect to MySQL server
查看>>
mysql case when 乱码_Mysql CASE WHEN 用法
查看>>
Multicast1
查看>>
mysql client library_MySQL数据库之zabbix3.x安装出现“configure: error: Not found mysqlclient library”的解决办法...
查看>>
MySQL Cluster 7.0.36 发布
查看>>
Multimodal Unsupervised Image-to-Image Translation多通道无监督图像翻译
查看>>
MySQL Cluster与MGR集群实战
查看>>
multipart/form-data与application/octet-stream的区别、application/x-www-form-urlencoded
查看>>
mysql cmake 报错,MySQL云服务器应用及cmake报错解决办法
查看>>
Multiple websites on single instance of IIS
查看>>
mysql CONCAT()函数拼接有NULL
查看>>
multiprocessing.Manager 嵌套共享对象不适用于队列
查看>>
multiprocessing.pool.map 和带有两个参数的函数
查看>>