设计模式之装饰器模式

装饰器模式

文章目录

定义

装饰模式(Decorator Pattern)是一种比较常见的模式,其定义如下:

Attach additional responsibilities to an object dynamically keeping the same interface.Decorators provide a flexible alternative to subclassing for extending functionality.(动态地给一个对象添加一些额外的职责。就增加功能来说,装饰模式相比生成子类更为灵活。)

优缺点

优点
  1. 装饰类和被装饰类可以独立发展,而不会相互耦合
  2. 装饰模式是继承关系的一个替代方案
  3. 装饰模式可以动态地扩展一个实现类的功能,这不需要多说,装饰模式的定义就是如此
缺点
  1. 需要扩展一个类的功能,或给一个类增加附加功能
  2. 需要动态地给一个对象增加功能,这些功能可以再动态地撤销
  3. 需要为一批的兄弟类进行改装或加装功能,当然是首选装饰模式

示例代码

  1. 定义接口或者抽象类

    java 复制代码
    public interface Component {
        void operate();
    }
  2. 抽象类实现上述接口

    java 复制代码
    public abstract class Decorator implements Component {
        private Component component;
    
        // 通过构造函数传递被修饰者
        public Decorator(Component component) {
            this.component = component;
        }
    
        @Override
        public void operate() {
            this.component.operate();
        }
    }
  3. 实现类

    java 复制代码
    public class ConcreteComponent implements Component {
        @Override
        public void operate() {
            System.out.println("do something");
        }
    }
  4. 具体实现类

    java 复制代码
    public class ConcreteDecorator1 extends Decorator {
        public ConcreteDecorator1(Component component) {
            super(component);
        }
    
        public void method1() {
            System.out.println("method1修饰");
        }
    
        // 重写父类operate方法
        @Override
        public void operate() {
            this.method1();
            super.operate();
        }
    }
    java 复制代码
    public class ConcreteDecorator2 extends Decorator {
        public ConcreteDecorator2(Component component) {
            super(component);
        }
    
        private void method2() {
            System.out.println("method2修饰");
        }
    
        @Override
        public void operate() {
            this.method2();
            super.operate();
        }
    }
  5. 测试方法

    java 复制代码
    @Test
    public void test() {
        Component component = new ConcreteComponent();
        // 第一次修饰
        component = new ConcreteDecorator1(component);
        // 第二次修饰
        component = new ConcreteDecorator2(component);
        // 修饰后运行
        component.operate();
    }

    运行结果如下:

    复制代码
    method2修饰
    method1修饰
    do something

    示例代码地址

    https://gitee.com/youxiaxiaomage/java-practices/tree/master/yxxmg-gof-sample/src/main/java/com/yxxmg/gof/structure/decorator

相关推荐
Kel16 小时前
MCP 传输链路全链路拆解:从字节流到协议栈的四层架构之旅
人工智能·设计模式·架构
atunet19 小时前
关于算法设计模式的演化与编程范式变迁的技术7
算法·设计模式
geovindu1 天前
go:Timing Functions Pattern
开发语言·后端·设计模式·golang·计时函数模式·性能分析模式
咖啡八杯3 天前
GoF设计模式——备忘录模式
java·后端·spring·设计模式
槑有老呆3 天前
从 Prompt Engineering 到 Harness Engineering:AI 编程的下一次跃迁
设计模式
HjhIron3 天前
从Prompt到Context:大模型应用开发的范式转移
设计模式·aigc·ai编程
咖啡八杯4 天前
GoF设计模式——中介者模式
java·后端·spring·设计模式
胡萝卜术5 天前
从“分数打架”到“排名投票”:为什么你的ChatBI必须用RRF?
算法·设计模式·面试
亦暖筑序6 天前
Java 8老系统Browser Agent实战:三层拦截把AI操作后台变成可审计流程
java·后端·设计模式
青禾网络8 天前
Web 前端如何接入 AI 音效生成:从零到可用的完整方案
人工智能·设计模式