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

结构型模式

适配器模式(Adapter Pattern)

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

适用场景

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

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

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

实现示例(Java)

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

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

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

    @Override
    public void request() {
        adaptee.specificRequest();
    }
}
4. 客户端代码
java 复制代码
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. 定义组件接口
java 复制代码
public interface Component {
    void operation();
}
2. 定义具体组件类
java 复制代码
public class ConcreteComponent implements Component {
    @Override
    public void operation() {
        System.out.println("ConcreteComponent operation");
    }
}
3. 定义装饰器抽象类
java 复制代码
public abstract class Decorator implements Component {
    protected Component component;

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

    @Override
    public void operation() {
        component.operation();
    }
}
4. 定义具体装饰器类
java 复制代码
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. 客户端代码
java 复制代码
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

总结

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

相关推荐
找了一圈尾巴3 小时前
设计模式-组合模式、模板模式
设计模式·组合模式
float_六七5 小时前
Java——单例类设计模式
java·单例模式·设计模式
老菜鸟的每一天5 小时前
创建型模式-Prototype 模式(原型模式)
设计模式·原型模式
码熔burning5 小时前
(五)趣学设计模式 之 建造者模式!
java·设计模式·建造者模式
黑不溜秋的14 小时前
C++ 设计模式 - 策略模式
c++·设计模式·策略模式
付聪121016 小时前
策略模式介绍和代码示例
设计模式
ThereIsNoCode17 小时前
「软件设计模式」状态模式(State)
设计模式·状态模式
菜鸟一枚在这1 天前
深入理解设计模式之代理模式
java·设计模式·代理模式
mjr1 天前
设计模式-Java
java·设计模式
yuanpan1 天前
23种设计模式之《组合模式(Composite)》在c#中的应用及理解
开发语言·设计模式·c#·组合模式