• Java设计模式之装饰器模式


    装饰器模式(Decorator Pattern)是一种结构型设计模式,它允许你在不改变对象结构的情况下,动态地将新功能附加到对象上。装饰器模式通过创建一个包装对象来实现这一目的,该包装对象包含了原始对象,并在其上添加了额外的行为。

    在装饰器模式中,有四个主要的角色:

    1. 抽象组件(Component):定义了装饰器和具体组件的共同接口,可以是抽象类或接口。
    2. 具体组件(Concrete Component):实现了抽象组件的接口,是被装饰的原始对象。
    3. 抽象装饰器(Decorator):继承自抽象组件,持有一个抽象组件的引用,并在其上添加额外的行为。
    4. 具体装饰器(Concrete Decorator):继承自抽象装饰器,实现了额外的行为。

    下面是一个简单的示例,展示了如何使用装饰器模式来扩展一个咖啡店的咖啡选择:

    // 抽象组件
    interface Coffee {
        String getDescription();
        double getCost();
    }
    
    // 具体组件
    class SimpleCoffee implements Coffee {
        @Override
        public String getDescription() {
            return "Simple Coffee";
        }
    
        @Override
        public double getCost() {
            return 1.0;
        }
    }
    
    // 抽象装饰器
    abstract class CoffeeDecorator implements Coffee {
        protected Coffee decoratedCoffee;
    
        public CoffeeDecorator(Coffee coffee) {
            this.decoratedCoffee = coffee;
        }
    
        @Override
        public String getDescription() {
            return decoratedCoffee.getDescription();
        }
    
        @Override
        public double getCost() {
            return decoratedCoffee.getCost();
        }
    }
    
    // 具体装饰器
    class MilkDecorator extends CoffeeDecorator {
        public MilkDecorator(Coffee coffee) {
            super(coffee);
        }
    
        @Override
        public String getDescription() {
            return decoratedCoffee.getDescription() + ", Milk";
        }
    
        @Override
        public double getCost() {
            return decoratedCoffee.getCost() + 0.5;
        }
    }
    
    // 具体装饰器
    class SugarDecorator extends CoffeeDecorator {
        public SugarDecorator(Coffee coffee) {
            super(coffee);
        }
    
        @Override
        public String getDescription() {
            return decoratedCoffee.getDescription() + ", Sugar";
        }
    
        @Override
        public double getCost() {
            return decoratedCoffee.getCost() + 0.2;
        }
    }
    
    // 使用示例
    public class DecoratorPatternExample {
        public static void main(String[] args) {
            // 创建一个简单咖啡对象
            Coffee coffee = new SimpleCoffee();
            System.out.println("Description: " + coffee.getDescription());
            System.out.println("Cost: $" + coffee.getCost());
    
            // 使用装饰器来添加牛奶和糖
            Coffee decoratedCoffee = new MilkDecorator(new SugarDecorator(coffee));
            System.out.println("Description: " + decoratedCoffee.getDescription());
            System.out.println("Cost: $" + decoratedCoffee.getCost());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86

    在上面的示例中,抽象组件是Coffee接口,具体组件是SimpleCoffee类,抽象装饰器是CoffeeDecorator类,具体装饰器有MilkDecoratorSugarDecorator类。我们可以通过组合不同的装饰器来动态地扩展咖啡的描述和价格,而不需要修改原始的咖啡对象。

    运行上述示例代码,输出如下:

    Description: Simple Coffee
    Cost: $1.0
    Description: Simple Coffee, Sugar, Milk
    Cost: $1.7
    
    • 1
    • 2
    • 3
    • 4

    可以看到,通过装饰器模式,我们成功地在不改变原始咖啡对象的情况下,动态地添加了牛奶和糖的描述和价格。这样的设计模式使得代码更加灵活、可扩展,并符合开闭原则。

  • 相关阅读:
    TeeChart for .NET 2023.10.19 Crack
    八股文--->Redis
    【分层强化学习】survey
    VUE3 之 插件的使用 - 这个系列的教程通俗易懂,适合自学
    MongoDB数据库入门到精通看这一篇就够了
    缓存和分布式锁笔记
    关于数据权限的设计
    通用表表达式查询
    docker入门
    百度网盘的扩容
  • 原文地址:https://blog.csdn.net/kkwyting/article/details/133702349