设计模式之装饰器模式

装饰器模式

文章目录

定义

装饰模式(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

相关推荐
YUEchn1 小时前
无处不在的Agent
设计模式·llm·agent
茶本无香3 小时前
设计模式之二—原型模式:灵活的对象克隆机制
java·设计模式·原型模式
GISer_Jing4 小时前
Nano Banana+LoveArt三大核心功能解析:重构AI设计全链路,让创意落地更高效
人工智能·设计模式·aigc
会员果汁5 小时前
14.设计模式-备忘录模式
设计模式·备忘录模式
xiaolyuh12315 小时前
Spring 框架 核心架构设计 深度详解
spring·设计模式·spring 设计模式
GISer_Jing1 天前
智能体工具使用、规划模式
人工智能·设计模式·prompt·aigc
GISer_Jing1 天前
AI Agent:学习与适应、模型上下文协议
人工智能·学习·设计模式·aigc
小马爱打代码1 天前
MyBatis设计模式:构建者、工厂、代理模式
设计模式·mybatis·代理模式
月明长歌1 天前
Javasynchronized 原理拆解:锁升级链路 + JVM 优化 + CAS 与 ABA 问题(完整整合版)
java·开发语言·jvm·安全·设计模式
会员果汁1 天前
12.设计模式-状态模式
设计模式·状态模式