【设计模式】装饰者模式里Java实现

插: AI时代 ,程序员或多或少要了解些人工智能,前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家(前言 -- 人工智能教程 )

坚持不懈,越努力越幸运,大家一起学习鸭~~~

装饰者设计模式(Decorator Pattern)是一种结构型设计模式,允许你动态地将新行为添加到对象实例中。这种模式通过创建一个包装类,也就是装饰者,来包裹原始的类,并在不改变其接口的情况下,增加新的功能。在Java中,装饰者模式广泛应用于需要扩展对象功能或动态地为对象添加功能的场景。

装饰者模式的基本结构

在装饰者模式中,通常有四个角色:

  1. Component(组件接口):定义一个对象接口,可以动态地给这些对象添加职责。

  2. ConcreteComponent(具体组件):实现Component接口的具体对象,是被装饰的原始对象。

  3. Decorator(装饰者抽象类):实现Component接口,并持有一个Component对象引用。这个类可以为Component对象动态添加功能。

  4. ConcreteDecorator(具体装饰者):扩展Decorator类的功能,具体实现装饰功能。

示例:在Java中实现一个简单的装饰者模式

假设我们有一个基础的咖啡接口和具体的咖啡实现类,然后我们想要为这些咖啡添加额外的调料(装饰者),比如牛奶或者糖浆。

Step 1: 定义组件接口
复制代码
// Component 接口
public interface Coffee {
    String getDescription();
    double getCost();
}
Step 2: 创建具体组件类
复制代码
// ConcreteComponent 具体咖啡类
public class SimpleCoffee implements Coffee {
    @Override
    public String getDescription() {
        return "Simple Coffee";
    }

    @Override
    public double getCost() {
        return 1.0;
    }
}
Step 3: 定义装饰者抽象类
复制代码
// Decorator 装饰者抽象类
public abstract class CoffeeDecorator implements Coffee {
    protected Coffee decoratedCoffee;

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

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

    public double getCost() {
        return decoratedCoffee.getCost();
    }
}
Step 4: 创建具体装饰者类
复制代码
// ConcreteDecorator 具体装饰者类 - 牛奶
public class MilkDecorator extends CoffeeDecorator {
    public MilkDecorator(Coffee coffee) {
        super(coffee);
    }

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

    @Override
    public double getCost() {
        return super.getCost() + 0.5;
    }
}
Step 5: 使用装饰者模式
复制代码
public class DecoratorPatternExample {
    public static void main(String[] args) {
        // 创建基础咖啡
        Coffee coffee = new SimpleCoffee();
        System.out.println("Cost: " + coffee.getCost() + "; Description: " + coffee.getDescription());

        // 加牛奶
        coffee = new MilkDecorator(coffee);
        System.out.println("Cost: " + coffee.getCost() + "; Description: " + coffee.getDescription());

        // 加糖浆
        coffee = new SyrupDecorator(coffee);
        System.out.println("Cost: " + coffee.getCost() + "; Description: " + coffee.getDescription());
    }
}

解释说明:

  • SimpleCoffee 实现了 Coffee 接口,表示基础的咖啡。

  • CoffeeDecorator 是装饰者的抽象类,持有一个 Coffee 对象的引用,通过组合的方式扩展 Coffee 的功能。

  • MilkDecorator 和 SyrupDecorator 分别是具体的装饰者类,通过继承 CoffeeDecorator 并重写相关方法,实现对咖啡的装饰。

通过这种方式,我们可以动态地为咖啡添加不同的调料,而不需要修改原始的咖啡类。这种设计模式提供了灵活性和可扩展性,使得我们可以在运行时动态地添加功能,而不会影响到其他对象。

装饰者模式在Java中的应用不仅局限于咖啡例子,还可以用于文件流处理、UI组件的动态功能添加等各种场景,是一种非常有用的设计模式。

相关推荐
小羊没烦恼!4 天前
微服务化的基石——持续集成
java·大数据·word·powerpoint·.net
俊昭喜喜里4 天前
java中的继承和多态的区别
java
小羊没烦恼!4 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
譕痕4 天前
JSONObject与JSONArray封装数据格式区别
java·json
胡写代码4 天前
别再前后端各写一套表单校验了
java·后端
小鱼能吃糖4 天前
缺陷修复总览 · mall电商项目:5类缺陷,1个病根,4个业务域
java·电商
此时不提桶,更待何时4 天前
01-06-A-JVM排查实战详解
java·jvm
伞伞悦读4 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
vipxieliang4 天前
ValidX 在 DDD 领域驱动设计中的实践
java·spring boot
C语言小火车4 天前
C/C++ 为什么需要编译器?
开发语言·c++