Java设计模式之装饰器模式

装饰器模式是一种结构型设计模式,它允许动态地将责任附加到对象上。装饰器模式是通过创建一个包装对象,也就是装饰器,来包裹真实对象,从而实现对真实对象的功能增强。装饰器模式可以在不修改原有对象的情况下,动态地添加功能。

装饰器模式通常由以下几个组件组成:

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

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

3.Decorator(装饰器抽象类):持有一个指向组件对象的引用,并实现了组件接口。通常是一个抽象类,用于定义装饰器的基本行为。

4.ConcreteDecorator(具体装饰器):扩展了装饰器抽象类,实现了具体的装饰功能。

开始上代码

首先我们定义一个咖啡

java 复制代码
// 组件接口
interface Coffee {
    double cost();
    String getDescription();
}

咖啡分很多种类,我们先来一个浓缩咖啡

java 复制代码
// 具体组件 - 浓缩咖啡
class Espresso implements Coffee {
    @Override
    public double cost() {
        return 1.5;
    }

    @Override
    public String getDescription() {
        return "Espresso";
    }
}

然后我觉得浓缩咖啡有点苦,所以我想加点糖和牛奶

所以这里我们要开始上装饰器了

java 复制代码
// 装饰器抽象类
abstract class CoffeeDecorator implements Coffee {
    protected Coffee coffee;

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

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

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

加奶的装饰器

java 复制代码
// 具体装饰器 - 加牛奶
class Milk extends CoffeeDecorator {
    public Milk(Coffee coffee) {
        super(coffee);
    }

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

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

加糖的装饰器

java 复制代码
// 具体装饰器 - 加糖
class Sugar extends CoffeeDecorator {
    public Sugar(Coffee coffee) {
        super(coffee);
    }

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

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

然后测试一下

java 复制代码
public class Test {

    public static void main(String[] args) {
        // 点一杯浓缩咖啡
        Coffee espresso = new Espresso();
        System.out.println("Espresso: " + espresso.getDescription() + " - Cost: $" + espresso.cost());

        // 加牛奶
        Coffee espressoWithMilk = new Milk(espresso);
        System.out.println("Espresso with Milk: " + espressoWithMilk.getDescription() + " - Cost: $" + espressoWithMilk.cost());

        // 加糖
        Coffee espressoWithSugar = new Sugar(espresso);
        System.out.println("Espresso with Sugar: " + espressoWithSugar.getDescription() + " - Cost: $" + espressoWithSugar.cost());

        // 加牛奶和糖
        Coffee espressoWithMilkAndSugar = new Sugar(new Milk(espresso));
        System.out.println("Espresso with Milk and Sugar: " + espressoWithMilkAndSugar.getDescription() + " - Cost: $" + espressoWithMilkAndSugar.cost());
    }
}

运行结果

可以发现这个模式很好理解,看起来也比较实用的样子

相关推荐
威风的虫6 分钟前
LangGraph的介绍
java·开发语言
康小庄12 分钟前
Java阻塞队列——用法及常用场景
java·开发语言·数据库·spring boot·spring·jetty
yy.y--17 分钟前
Java多线程实例:输出线程名20次
java·开发语言
SakitamaX1 小时前
Tomcat介绍与实验
java·tomcat
Y001112361 小时前
Day24—IO流-2
java·开发语言
历程里程碑1 小时前
普通数组---合并区间
java·大数据·数据结构·算法·leetcode·elasticsearch·搜索引擎
高斯林.神犇2 小时前
idea快捷键
java·ide·intellij-idea
青春易逝丶2 小时前
术语缩写
java