设计模式-适配器模式(Adapter Pattern)

1. 概念

适配器模式(Adapter Pattern)是一种结构型设计模式。适配器模式通过创建一个适配器类,将原本不兼容的接口连接起来,使得原本无法一起工作的类可以协同工作。适配器模式通常用于将现有的类与新的系统或功能进行集成。

2. 优点

  • 提高了类的复用性:通过适配器,可以重用现有的类,而无需修改其代码。
  • 解耦合:客户端与具体实现解耦,使得系统更灵活。
  • 易于扩展:可以通过添加新的适配器来支持新的接口,而不影响现有代码。

3. 缺点

  • 增加了复杂性:适配器模式可能会引入额外的类和对象,增加系统的复杂性。
  • 性能开销:适配器可能会引入额外的调用层,影响性能。

4. 组成部分

  • 目标接口(Target):客户端所期望的接口。
  • 源类(Adaptee):需要适配的类,拥有不兼容的接口。
  • 适配器(Adapter):实现目标接口,并持有源类的实例,将源类的方法转换为目标接口的方法。

5. 使用场景

  • 已有类的接口不符合需求:当需要使用的类的接口与当前系统不兼容时。
  • 多种接口的统一:将多个不同的接口统一成一个接口,提供给客户端使用。
  • 第三方库的集成:在集成第三方库时,适配器可以将其接口转换为应用程序的接口。

6. 代码举例

6.1 对象适配器

目标接口

public interface Target {
    void request();
}

源类

public class Adaptee {
    public void specificRequest() {
        System.out.println("Specific request from Adaptee.");
    }
}

适配器类

public class Adapter implements Target {
    private Adaptee adaptee;

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

    @Override
    public void request() {
        adaptee.specificRequest(); // 调用源类的方法
    }
}

客户端代码

public class ObjectAdapterClient {
    public static void main(String[] args) {
        Adaptee adaptee = new Adaptee();
        Target target = new Adapter(adaptee);
        target.request(); // 输出: Specific request from Adaptee.
    }
}

在这里插入图片描述

6.2 类适配器

目标接口

public interface Target {
    void request();
}

源类

public class Adaptee {
    public void specificRequest() {
        System.out.println("Specific request from Adaptee.");
    }
}

适配器类(类适配器)

public class Adapter extends Adaptee implements Target {
    @Override
    public void request() {
        specificRequest(); // 直接调用源类的方法
    }
}

客户端代码

public class ClassAdapterClient {
    public static void main(String[] args) {
        Target target = new Adapter();
        target.request(); // 输出: Specific request from Adaptee.
    }
}

在这里插入图片描述

6.3 接口适配器

接口

public interface Interface {
    void method1();
    void method2();
    void method3();
}

适配器类

public class Adapter implements Interface {
    @Override
    public void method1() {
        // 默认实现,留空
    }

    @Override
    public void method2() {
        // 默认实现,留空
    }

    @Override
    public void method3() {
        // 默认实现,留空
    }
}

具体实现类

public class ConcreteClass extends Adapter {
    @Override
    public void method1() {
        System.out.println("Method1 implementation.");
    }

    // method2 和 method3 使用默认实现
}

客户端代码

public class InterfaceAdapterClient {
    public static void main(String[] args) {
        Interface obj = new ConcreteClass();
        obj.method1(); // 输出: Method1 implementation.
        obj.method2(); // 不输出任何内容,因为使用了默认实现
    }
}

在这里插入图片描述

7. 总结

  • 对象适配器:通过持有源类的实例实现目标接口,适合在运行时动态绑定。
  • 类适配器:通过继承源类实现目标接口,适合在编译时确定适配关系。
  • 接口适配器:允许实现接口的一部分方法,适合接口方法较多且只需要实现部分方法的情况。

适配器模式是解决接口不兼容问题的有效工具,能够提高代码的复用性和灵活性。尽管它可能增加系统的复杂性,但在许多场景中,它提供了一种优雅的解决方案,使得不同接口之间能够顺利协作。在设计系统时,合理使用适配器模式可以显著提升系统的可维护性和扩展性。

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值