结构型设计模式(二)装饰器模式 & 适配器模式

装饰器模式 Decorator

1、什么是装饰器模式

装饰器模式允许通过将对象放入特殊的包装对象中来为原始对象添加新的行为。这种模式是一种结构型模式,因为它通过改变结构来改变被装饰对象的行为。它涉及到一组装饰器类,这些类用来包装具体组件。

2、为什么使用装饰器模式

  1. 灵活性:装饰器模式允许在运行时动态地为对象添加新的行为,而无需修改其代码,提供了一种灵活的方式来扩展对象的功能。
  2. 避免子类爆炸:通过使用装饰器模式,可以避免创建大量子类来扩展对象的功能,从而避免了子类爆炸的问题。
  3. 组合功能:可以通过组合多个装饰器来实现复杂的功能组合,无需使用大量的继承关系。

3、如何使用装饰器模式

设计实现咖啡订单系统,包含基本咖啡和不同的配料作为装饰器

java 复制代码
// 抽象组件 - 咖啡
interface Coffee {
    String getDescription();
    double cost();
}

// 具体组件 - 基本咖啡
class BasicCoffee implements Coffee {
    @Override
    public String getDescription() {
        return "Basic Coffee";
    }

    @Override
    public double cost() {
        return 3.0;
    }
}

// 抽象装饰器
abstract class CoffeeDecorator implements Coffee {
    protected Coffee decoratedCoffee;

    public CoffeeDecorator(Coffee decoratedCoffee) {
        this.decoratedCoffee = decoratedCoffee;
    }

    @Override
    public String getDescription() {
        return decoratedCoffee.getDescription();
    }

    @Override
    public double cost() {
        return decoratedCoffee.cost();
    }
}

// 具体装饰器 - 牛奶
class MilkDecorator extends CoffeeDecorator {
    public MilkDecorator(Coffee decoratedCoffee) {
        super(decoratedCoffee);
    }

    @Override
    public String getDescription() {
        return super.getDescription() + ", Milk";
    }

    @Override
    public double cost() {
        return super.cost() + 1.0;
    }
}

// 具体装饰器 - 糖
class SugarDecorator extends CoffeeDecorator {
    public SugarDecorator(Coffee decoratedCoffee) {
        super(decoratedCoffee);
    }

    @Override
    public String getDescription() {
        return super.getDescription() + ", Sugar";
    }

    @Override
    public double cost() {
        return super.cost() + 0.5;
    }
}

// 客户端代码
public class Client {
    public static void main(String[] args) {
        // 创建基本咖啡
        Coffee basicCoffee = new BasicCoffee();
        System.out.println("Description: " + basicCoffee.getDescription());
        System.out.println("Cost: $" + basicCoffee.cost());

        // 添加牛奶装饰器
        Coffee milkCoffee = new MilkDecorator(basicCoffee);
        System.out.println("Description: " + milkCoffee.getDescription());
        System.out.println("Cost: $" + milkCoffee.cost());

        // 添加糖装饰器
        Coffee sugarMilkCoffee = new SugarDecorator(milkCoffee);
        System.out.println("Description: " + sugarMilkCoffee.getDescription());
        System.out.println("Cost: $" + sugarMilkCoffee.cost());
    }
}

4、是否存在缺陷和不足

  1. 可能导致类爆炸:有大量具体组件和装饰器时,可能导致类的数量急剧增加,增加了系统的复杂性。
  2. 破坏封装性:装饰器模式将具体组件暴露给装饰器类,可能破坏了封装性。

5、如何缓解缺陷和不足

  1. 使用抽象工厂:结合抽象工厂模式,通过工厂来创建组件和装饰器,降低类的数量。
  2. 使用组合模式:将具体组件和装饰器组织成树形结构,使用组合模式来管理它们的关系。
  3. 慎用过多装饰器:在设计时慎用过多的装饰器,确保仅在需要时使用,以避免类爆炸问题。

适配器模式 Adapter

1、什么是适配器模式

适配器模式允许原本由于接口不匹配而无法在一起工作的类能够协同工作。它通过引入一个包装类,即适配器,来转换原有类的接口为客户端期望的接口。

2、为什么使用适配器模式

  1. 解耦性:适配器模式允许客户端与目标类的实现细节解耦,使得客户端不需要知道目标类的内部实现。
  2. 复用性:适配器模式可以使得已有的类在新的系统中复用,而无需修改其代码。
  3. 灵活性:适配器模式允许在不改变现有代码的情况下引入新的类,提高系统的灵活性。

3、如何使用适配器模式

csharp 复制代码
// 目标接口
interface Target {
    void request();
}

// 不兼容的类
class Adaptee {
    public void specificRequest() {
        System.out.println("Adaptee's specificRequest");
    }
}

// 适配器类
class Adapter implements Target {
    private Adaptee adaptee;

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

    @Override
    public void request() {
        adaptee.specificRequest();
    }
}

// 客户端代码
public class Client {
    public static void main(String[] args) {
        Adaptee adaptee = new Adaptee();
        Target target = new Adapter(adaptee);

        target.request();
    }
}

4、是否存在缺陷和不足

  1. 可能导致过多的适配类:如果系统中有多个不同的类需要适配,可能会导致大量的适配器类,使系统变得复杂。
  2. 不支持多继承的语言的限制:在一些不支持多继承的语言中,适配器模式可能会受到限制。

5、如何缓解缺陷和不足

  1. 使用对象适配器而非类适配器:对象适配器通过组合的方式引入被适配对象,避免了类适配器的多继承问题。
  2. 考虑使用接口适配器:如果目标接口中定义的方法较多,可以考虑使用接口适配器模式,只需实现感兴趣的方法。
相关推荐
ssshooter4 小时前
Replit 项目本地跑:一份实测过的改造清单
前端·后端·设计模式
饮水桶_T10 小时前
Java调用大语言模型、多轮对话 笔记
笔记·算法·设计模式·电脑·ai编程
AI酒旅实战开发2 天前
旅行 AI Agent 的 7 种设计模式:从工具调用到交易闭环
人工智能·设计模式
geovindu3 天前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
geovindu4 天前
rust: Timing Functions Pattern
开发语言·后端·设计模式·rust·计时函数模式·性能分析型模式
Mh9 天前
我是如何设计品牌站组件库的?
前端·css·设计模式
是2的10次方啊10 天前
设计模式与生活·总论:AI 能替你砌墙,替不了你打地基
设计模式
dahaihua33310 天前
用 SysML 表达航电系统架构(附建模示例)
设计模式·重构·系统架构·集成测试·需求分析·学习方法
wy_hhxx11 天前
《Agent设计模式 图解可复用智能体架构》上篇内容整理
设计模式·架构·ai agent
AAA修煤气灶刘哥11 天前
DSH Super-agent 双层架构:如何兼顾后台执行与持续响应
人工智能·后端·设计模式