java设计模式:03-04-装饰器模式

装饰器模式(Decorator Pattern)

装饰器模式(Decorator Pattern)是一种结构型设计模式,它允许向一个现有的对象添加新的功能,同时又不改变其结构。装饰器模式通过创建一个装饰类来包装原有的类,并在保留原有类的接口的情况下,提供额外的功能。

装饰器模式的应用场景

  • 在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责:如给一个图形对象添加边框、颜色等属性。
  • 当不能采用生成子类的方法进行扩展时:如有大量独立的扩展,或扩展是动态的。

装饰器模式的实现方式

1. 传统实现方式

思想:通过定义一个抽象的组件类,所有的具体组件和装饰类都继承自该组件类。装饰类包含一个组件对象,并在调用其方法前后进行增强。

实现方式

java 复制代码
// 抽象组件
abstract class Component {
    public abstract void operation();
}

// 具体组件
class ConcreteComponent extends Component {
    public void operation() {
        System.out.println("ConcreteComponent operation");
    }
}

// 抽象装饰类
abstract class Decorator extends Component {
    protected Component component;

    public Decorator(Component component) {
        this.component = component;
    }

    public void operation() {
        component.operation();
    }
}

// 具体装饰类A
class ConcreteDecoratorA extends Decorator {
    public ConcreteDecoratorA(Component component) {
        super(component);
    }

    public void operation() {
        super.operation();
        addedBehavior();
    }

    public void addedBehavior() {
        System.out.println("ConcreteDecoratorA added behavior");
    }
}

// 具体装饰类B
class ConcreteDecoratorB extends Decorator {
    public ConcreteDecoratorB(Component component) {
        super(component);
    }

    public void operation() {
        super.operation();
        addedBehavior();
    }

    public void addedBehavior() {
        System.out.println("ConcreteDecoratorB added behavior");
    }
}

// 客户端代码
public class DecoratorPattern {
    public static void main(String[] args) {
        Component component = new ConcreteComponent();
        Component decoratorA = new ConcreteDecoratorA(component);
        Component decoratorB = new ConcreteDecoratorB(decoratorA);
        decoratorB.operation();
    }
}

优点

  • 可以动态地添加和删除对象的职责,比静态继承更加灵活。
  • 通过使用不同的具体装饰类以及这些装饰类的排列组合,可以创造出很多不同行为的组合。
  • 符合开闭原则,可以在不修改原有代码的情况下扩展对象的新功能。

缺点

  • 会产生许多小对象,增加了系统的复杂性。
  • 多层装饰时会增加调试的难度,特别是出现问题时难以找到问题所在。
2. 使用接口实现装饰器模式

思想:通过定义一个接口,所有的具体组件和装饰类都实现该接口,装饰类包含一个接口类型的对象,并在调用其方法前后进行增强。

实现方式

java 复制代码
// 抽象组件接口
interface Component {
    void operation();
}

// 具体组件
class ConcreteComponent implements Component {
    public void operation() {
        System.out.println("ConcreteComponent operation");
    }
}

// 抽象装饰类
abstract class Decorator implements Component {
    protected Component component;

    public Decorator(Component component) {
        this.component = component;
    }

    public void operation() {
        component.operation();
    }
}

// 具体装饰类A
class ConcreteDecoratorA extends Decorator {
    public ConcreteDecoratorA(Component component) {
        super(component);
    }

    public void operation() {
        super.operation();
        addedBehavior();
    }

    public void addedBehavior() {
        System.out.println("ConcreteDecoratorA added behavior");
    }
}

// 具体装饰类B
class ConcreteDecoratorB extends Decorator {
    public ConcreteDecoratorB(Component component) {
        super(component);
    }

    public void operation() {
        super.operation();
        addedBehavior();
    }

    public void addedBehavior() {
        System.out.println("ConcreteDecoratorB added behavior");
    }
}

// 客户端代码
public class InterfaceDecoratorPattern {
    public static void main(String[] args) {
        Component component = new ConcreteComponent();
        Component decoratorA = new ConcreteDecoratorA(component);
        Component decoratorB = new ConcreteDecoratorB(decoratorA);
        decoratorB.operation();
    }
}

优点

  • 接口使得装饰类和具体组件更加解耦,符合依赖倒置原则。
  • 接口可以更容易地进行单元测试和模拟对象。

缺点

  • 同样会产生许多小对象,增加系统复杂性。
  • 多层装饰时仍然会增加调试的难度。

总结

实现方式 优点 缺点
传统实现方式 动态添加和删除职责,符合开闭原则,行为组合多样化 产生许多小对象,系统复杂性增加,多层装饰调试难度大
使用接口实现装饰器模式 解耦装饰类和具体组件,符合依赖倒置原则,易于单元测试和模拟对象 产生许多小对象,系统复杂性增加,多层装饰调试难度大

选择哪种实现方式应根据具体的需求和系统的复杂度来决定。如果希望组件和装饰类之间更加解耦且易于测试,可以选择使用接口实现装饰器模式。如果更关注具体类的扩展性和灵活性,可以选择传统的实现方式。

相关推荐
摇滚侠21 分钟前
Spring Boot3零基础教程,SpringApplication 自定义 banner,笔记54
java·spring boot·笔记
青云交24 分钟前
Java 大视界 -- Java 大数据机器学习模型在游戏用户行为分析与游戏平衡优化中的应用
java·大数据·机器学习·数据存储·模型构建·游戏用户行为分析·游戏平衡优化
暗武逢天3 小时前
Java导出写入固定Excel模板数据
java·导出数据·easyexcel·excel固定模板导出
摇滚侠3 小时前
Spring Boot3零基础教程,KafkaTemplate 发送消息,笔记77
java·spring boot·笔记·后端·kafka
杯莫停丶4 小时前
设计模式之:模板模式
开发语言·设计模式
lapiii3584 小时前
14天极限复习软考day4-法律、设计模式
设计模式
fat house cat_6 小时前
【netty】基于主从Reactor多线程模型|如何解决粘包拆包问题|零拷贝
java·服务器·网络·netty
青云交7 小时前
Java 大视界 -- Java 大数据在智能教育学习社区互动模式创新与用户活跃度提升中的应用(426)
java·大数据·学习·flink 实时计算·智能教育社区·互动模式创新·用户活跃度
神奇的海马体7 小时前
Tomcat隐藏版本号
java·tomcat
拜见老天師7 小时前
使用mybatis-plus,实现将排序时,字段值为NULL的数据排在最后
java·mybatis