结构型设计模式 - 装饰者模式

本文介绍了装饰者模式,一种在不改变对象结构的情况下动态添加新行为的设计模式,涉及组件、具体组件、装饰者和具体装饰者的角色,以及其在代码中的应用和优缺点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

装饰者模式是一种结构型设计模式(亦称: 装饰器模式、Wrapper、Decorator),允许你在不改变对象自身结构的情况下,动态地给对象添加新的行为或责任。它通过将对象放入包装器中来实现这一目的,每个包装器(装饰者)都实现了与被包装对象相同的接口,并且可以在运行时动态添加或修改功能。

结构

在装饰者模式中,有几个核心角色:

  • Component(组件):定义了被装饰对象的接口,可以是抽象类或接口。
  • ConcreteComponent(具体组件):实现了Component接口,是被装饰的对象。
  • Decorator(装饰者):也实现了Component接口,持有一个Component实例,并且可以通过构造函数或其他方式将被装饰对象包裹起来。Decorator通常是一个抽象类,它的具体子类实现具体的装饰逻辑。
  • ConcreteDecorator(具体装饰者):继承自Decorator,实现了具体的装饰行为,可以在被装饰对象的行为前后添加自定义的行为。

在这里插入图片描述
以上是一个模拟咖啡店中咖啡的装饰过程,Coffee是一个接口,SimpleCoffee是实现了Coffee接口的具体组件。CoffeeDecorator是装饰者抽象类,MilkDecorator是具体的装饰者,实现了添加牛奶的功能

代码示例

Coffee:组件

public interface Coffee {
    double getCost();

    String getIngredients();
}

SimpleCoffee:具体组件

public class SimpleCoffee implements Coffee {
    public double getCost() {
        return 1.0;
    }

    public String getIngredients() {
        return "Coffee";
    }
}

CoffeeDecorator:装饰者

public abstract class CoffeeDecorator implements Coffee {
    protected final Coffee decoratedCoffee;

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

    public double getCost() {
        return decoratedCoffee.getCost();
    }

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

MilkDecorator:具体装饰者

public class MilkDecorator extends CoffeeDecorator {
    public MilkDecorator(Coffee decoratedCoffee) {
        super(decoratedCoffee);
    }

    public double getCost() {
        return super.getCost() + 0.5;
    }

    public String getIngredients() {
        return super.getIngredients() + ", Milk";
    }
}

运行示例:

    public static void main(String[] args) {
        // Simple Coffee
        Coffee coffee = new SimpleCoffee();
        System.out.println("Cost: " + coffee.getCost() + ", Ingredients: " + coffee.getIngredients());

        // Coffee with Milk
        Coffee coffeeWithMilk = new MilkDecorator(new SimpleCoffee());
        System.out.println("Cost: " + coffeeWithMilk.getCost() + ", Ingredients: " + coffeeWithMilk.getIngredients());
    }

优点

  • 灵活性:可以动态地给对象添加新功能,不需要修改现有代码。
  • 可扩展性:可以通过组合装饰器的方式实现不同组合的行为。
  • 遵循开闭原则:无需修改现有代码即可添加新功能

缺点

  • 可能产生过多小对象:装饰者模式会增加许多具体装饰者类,可能导致类的数量增加,不易管理。

适用场景

  • 需要动态地给对象添加功能,而且要求这些功能可以灵活组合的情况。
  • 不希望通过继承来扩展功能,因为继承会造成静态的行为,而装饰者模式能够在运行时动态添加行为。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值