• Java23种设计模式-结构型模式之装饰器模式


    装饰器模式(Decorator Pattern):动态地给一个对象添加一些额外的职责。这种模式提供了一种灵活的方式来扩展对象的功能,而不需要修改原始类的代码
    通常有以下角色:
    角色1.抽象组件(Component):定义对象的基本接口
    角色2.具体组件(Concrete Component):实现抽象组件的具体类
    角色3.抽象装饰器(Decorator):继承抽象组件,用于添加额外功能
    角色4.具体装饰器(Concrete Decorator):实现抽象装饰器具体添加特定的功能。

    优点包括:
    1.灵活性:可以在运行时动态地添加或移除功能。
    2.可扩展性无需修改原始类,就能添加新的装饰器
    3.复用性可以重复使用装饰器来构建不同功能组合
    4.独立性:装饰器与具体类解耦,更易于测试和维护。

    应用场景
    1.扩展性需求:当需要在不修改现有代码的基础上,为对象添加新的功能或行为时。
    2.功能组合:可以将多个装饰器组合在一起,以构建复杂的功能。
    3.动态配置:在运行时根据具体情况动态地添加或移除装饰器。
    4.独立扩展:不同的装饰器可以独立进行扩展和修改,互不影响。
    5.性能优化:通过添加装饰器来优化对象的性能。
    例如
    1.图形界面组件:可以为组件添加边框、颜色、阴影等装饰。
    2.文件操作如添加加密、压缩等功能
    3.数据库操作例如添加日志记录、性能监控等
    4.网络通信:如添加数据加密、协议转换等。
    5.游戏角色:为角色添加各种技能或特效。

    示例:定义了一个Component接口,它有一个operation方法。然后我们创建了一个具体的组件ConcreteComponent,它实现了Component接口。
    接着,我们定义了一个Decorator抽象类,它同样实现了Component接口,并持有一个Component类型的成员变量。Decorator类的operation方法会委托给这个成员变量。
    之后,我们创建了两个具体的装饰器ConcreteDecoratorA和ConcreteDecoratorB,它们都继承自Decorator类,并且各自添加了不同的行为。
    最后具体整合测试

    //定义组价接口
    public interface Component {
        void operation();
    }
    // 定义具体组件
    public class ConcreteComponent implements Component{
        @Override
        public void operation() {
            System.out.println("ConcreteComponent operation executed.");
        }
    }
    // 定义装饰器抽象类
    public class Decorator implements Component{
        protected Component component; // 被装饰的对象
    
        public Decorator(Component component) {
            this.component = component;
        }
        @Override
        public void operation() {
            component.operation();
        }
    }
    // 定义具体装饰器A
    public class ConcreteDecoratorA extends Decorator{
        public ConcreteDecoratorA(Component component) {
            super(component);
        }
        @Override
        public void operation() {
            super.operation();
            addedBehavior(); // 添加的行为
        }
    
        private void addedBehavior() {
            System.out.println("ConcreteDecoratorA added behavior executed.");
        }
    
    }
    // 定义具体装饰器B
    public class ConcreteDecoratorB extends Decorator{
        public ConcreteDecoratorB(Component component) {
            super(component);
        }
    
        @Override
        public void operation(){
            super.operation();
            anotherAddedBehavior(); // 另一个添加的行为
        }
        private void anotherAddedBehavior() {
            System.out.println("ConcreteDecoratorB added behavior executed.");
        }
    }
    //具体整合测试
    public class DecoratorPatternDemo {
        public static void main(String[] args) {
            Component component=new ConcreteComponent();
            Component decoratorA=new ConcreteDecoratorA(component);
            Component decoratorB=new ConcreteDecoratorB(decoratorA);
    
            decoratorB.operation();
        }
    }
    
    • 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
  • 相关阅读:
    P3916 图的遍历
    Java 多线程共享模型之管程(下)
    网络练习题
    StopWatch使用注意S
    广州华锐互动:VR模拟高楼层建筑应急逃生,提供身临其境的虚拟体验
    在VS Code 中调试远程服务器的PHP代码
    acwing算法基础之基础算法--高精度除法算法
    操作系统第三章王道习题_内存管理_总结易错知识点
    nextJs13:如何全局配置antd的日期组件datepicker等都显示中文(2步)
    工具推荐#简单图片转换为ASCII图(基于字符)
  • 原文地址:https://blog.csdn.net/csy08845/article/details/138158190