设计模式使用场景实现示例(结构型模式——适配器模式、装饰器模式)

结构型模式

适配器模式(Adapter Pattern)

适配器模式(Adapter Pattern)是一种结构型设计模式,它允许将一个类的接口转换成客户端所期待的另一个接口。适配器模式使得原本由于接口不兼容而不能一起工作的类可以协同工作。

适用场景

  1. 希望复用一些现存的类,但接口不符合要求

    • 系统中有多个接口相似的类,但接口不完全相同,需要一个统一的接口来调用这些类。
  2. 希望使用一些已有的类,但它们的接口与需求接口不一致

    • 使用第三方库或老旧系统中的类,但它们的接口和当前系统的不一致。

实现示例(Java)

以下是一个简单的适配器模式的实现示例,展示如何将一个不兼容的接口适配到一个目标接口。

1. 定义目标接口
public interface Target {
    void request();
}
2. 定义适配者类(Adaptee)
public class Adaptee {
    public void specificRequest() {
        System.out.println("Called specificRequest()");
    }
}
3. 定义适配器类(Adapter)
public class Adapter implements Target {
    private Adaptee adaptee;

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

    @Override
    public void request() {
        adaptee.specificRequest();
    }
}
4. 客户端代码
public class Client {
    public static void main(String[] args) {
        Adaptee adaptee = new Adaptee();
        Target target = new Adapter(adaptee);
        
        target.request();
    }
}

注释说明

  1. 目标接口

    • Target 接口定义了客户所期待的接口,包含一个 request 方法。
  2. 适配者类

    • Adaptee 类包含一个 specificRequest 方法,这是需要适配的接口。
  3. 适配器类

    • Adapter 类实现了 Target 接口,并持有一个 Adaptee 对象。它在 request 方法中调用了 AdapteespecificRequest 方法,以实现接口的适配。
  4. 客户端代码

    • Client 类中创建了 AdapteeAdapter 对象,通过 Adapter 对象调用 request 方法,从而实现接口的适配。

类图

Client ----> Target
              ^
              |
          Adapter -------> Adaptee

总结

适配器模式通过引入一个适配器类,将原本不兼容的接口适配到目标接口,使得原本不能一起工作的类可以协同工作。它适用于复用现有类但接口不兼容的场景,能够提高代码的复用性和灵活性。

装饰器模式(Decorator Pattern)

装饰器模式(Decorator Pattern)是一种结构型设计模式,它允许你动态地为对象添加新的行为,而无需改变其代码。装饰器模式通过创建一个装饰类来包装原始类,从而增强或改变其功能。

适用场景

  1. 需要扩展一个类的功能

    • 需要为一个类添加一些额外的职责,而不希望修改原始类的代码。
  2. 动态添加功能

    • 希望通过组合多个装饰器动态地添加功能。
  3. 希望实现功能的按需组合

    • 通过多个装饰器可以组合出不同的功能组合,避免创建大量的子类。

实现示例(Java)

以下是一个简单的装饰器模式的实现示例,展示如何动态地为一个对象添加新功能。

1. 定义组件接口
public interface Component {
    void operation();
}
2. 定义具体组件类
public class ConcreteComponent implements Component {
    @Override
    public void operation() {
        System.out.println("ConcreteComponent operation");
    }
}
3. 定义装饰器抽象类
public abstract class Decorator implements Component {
    protected Component component;

    public Decorator(Component component) {
        this.component = component;
    }

    @Override
    public void operation() {
        component.operation();
    }
}
4. 定义具体装饰器类
public class ConcreteDecoratorA extends Decorator {
    public ConcreteDecoratorA(Component component) {
        super(component);
    }

    @Override
    public void operation() {
        super.operation();
        addedBehavior();
    }

    private void addedBehavior() {
        System.out.println("ConcreteDecoratorA added behavior");
    }
}

public class ConcreteDecoratorB extends Decorator {
    public ConcreteDecoratorB(Component component) {
        super(component);
    }

    @Override
    public void operation() {
        super.operation();
        addedBehavior();
    }

    private void addedBehavior() {
        System.out.println("ConcreteDecoratorB added behavior");
    }
}
5. 客户端代码
public class Client {
    public static void main(String[] args) {
        Component component = new ConcreteComponent();
        Component decoratorA = new ConcreteDecoratorA(component);
        Component decoratorB = new ConcreteDecoratorB(decoratorA);

        decoratorB.operation();
    }
}

注释说明

  1. 组件接口

    • Component 接口定义了一个方法 operation,这是所有组件和装饰器都需要实现的方法。
  2. 具体组件类

    • ConcreteComponent 是一个具体的组件类,实现了 Component 接口的方法。
  3. 装饰器抽象类

    • Decorator 是一个抽象类,实现了 Component 接口,并持有一个 Component 对象。它的 operation 方法调用了被装饰对象的 operation 方法。
  4. 具体装饰器类

    • ConcreteDecoratorAConcreteDecoratorB 是具体的装饰器类,它们继承自 Decorator 并添加了新的行为。
  5. 客户端代码

    • Client 类通过组合多个装饰器来增强 ConcreteComponent 的功能。decoratorB 包装了 decoratorA,而 decoratorA 包装了 component,最终调用 operation 方法时,会依次调用各个装饰器和具体组件的方法。

类图

Client
  |
  v
Component <---- Decorator <---- ConcreteDecoratorA
  ^                                |
  |                                v
ConcreteComponent          ConcreteDecoratorB

总结

装饰器模式通过创建装饰器类来包装原始类,使得你可以动态地添加或改变对象的行为。它适用于需要扩展类的功能且不希望修改原始类代码的场景,同时通过组合多个装饰器可以实现功能的按需组合,避免创建大量的子类。

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-07-11 20:48:05       67 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-07-11 20:48:05       71 阅读
  3. 在Django里面运行非项目文件

    2024-07-11 20:48:05       58 阅读
  4. Python语言-面向对象

    2024-07-11 20:48:05       69 阅读

热门阅读

  1. 代码随想录-DAY⑦-字符串——leetcode 344 | 541 | 151

    2024-07-11 20:48:05       21 阅读
  2. FastAPI+SQLAlchemy数据库连接

    2024-07-11 20:48:05       19 阅读
  3. 关于vue监听数组

    2024-07-11 20:48:05       18 阅读
  4. SQL 自定义函数

    2024-07-11 20:48:05       22 阅读
  5. linux内核访问读写用户层文件方法

    2024-07-11 20:48:05       21 阅读
  6. RK3568平台开发系列讲解(网络篇)netfilter框架

    2024-07-11 20:48:05       19 阅读
  7. Netty服务端接收TCP链接数据

    2024-07-11 20:48:05       16 阅读
  8. 【面试题】Golang (第一篇)

    2024-07-11 20:48:05       22 阅读