Java—装饰器模式

介绍

装饰器模式

装饰器模式(Decorator Pattern)是一种结构型设计模式,它允许你动态地将行为添加到现有的对象中,而无需修改其代码。装饰器模式提供了比继承更灵活的功能扩展方式。

主要角色
  1. Component:定义一个对象接口,可以给这些对象动态地添加职责。
  2. ConcreteComponent:具体实现Component接口的类,即被装饰器装饰的原始对象。
  3. Decorator:装饰器基类,持有一个Component对象的引用,并且与Component接口保持一致。
  4. ConcreteDecorator:具体的装饰器类,实现具体要向Component添加的功能。

Java代码实现示例

我们将通过一个简单的例子来演示装饰器模式。假设我们有一个基本的消息发送系统,能够发送简单的文本消息。现在我们希望能够在发送消息之前对消息进行加密,并在发送之后记录日志。

定义组件接口和具体组件
java 复制代码
// Component
interface Message {
    String send();
}

// ConcreteComponent 被装饰者
class TextMessage implements Message {
    private String content;

    public TextMessage(String content) {
        this.content = content;
    }

    @Override
    public String send() {
        return "Sending message: " + content;
    }
}
定义装饰器基类和具体装饰器
java 复制代码
// Decorator 装饰者 (增强)
abstract class MessageDecorator implements Message {
    protected Message wrapped;

    public MessageDecorator(Message wrapped) {
        this.wrapped = wrapped;
    }

    @Override
    public String send() {
        return wrapped.send();
    }
}

// ConcreteDecoratorA
class EncryptedMessageDecorator extends MessageDecorator {
    public EncryptedMessageDecorator(Message wrapped) {
        super(wrapped);
    }

    @Override
    public String send() {
        String originalMessage = wrapped.send();
        String encryptedMessage = encrypt(originalMessage);
        return "Encrypted(" + encryptedMessage + ")";
    }

    private String encrypt(String message) {
        // 简单的模拟加密逻辑
        StringBuilder encrypted = new StringBuilder(message);
        return encrypted.reverse().toString();
    }
}

// ConcreteDecoratorB
class LoggedMessageDecorator extends MessageDecorator {
    public LoggedMessageDecorator(Message wrapped) {
        super(wrapped);
    }

    @Override
    public String send() {
        String result = wrapped.send();
        log(result);
        return result;
    }

    private void log(String message) {
        System.out.println("Logging: " + message);
    }
}
使用装饰器模式
java 复制代码
public class Main {
    public static void main(String[] args) {
        Message message = new TextMessage("Hello, World!");

        // 使用加密装饰器
        Message encryptedMessage = new EncryptedMessageDecorator(message);
        System.out.println(encryptedMessage.send());  // 输出加密后的消息

        // 使用日志装饰器
        Message loggedMessage = new LoggedMessageDecorator(message);
        System.out.println(loggedMessage.send());  // 日志消息并输出

        // 组合装饰器
        Message encryptedLoggedMessage = new LoggedMessageDecorator(new EncryptedMessageDecorator(message));
        System.out.println(encryptedLoggedMessage.send());  // 输出加密并记录日志的消息
    }
}

运行结果

解释

  1. Message接口 :定义了发送消息的方法 send。
  2. TextMessage类:实现了Message接口,表示一个简单的文本消息。
  3. MessageDecorator抽象类:也是Message接口的实现,但它持有一个Message对象,并在其方法调用时委托给该对象。
  4. EncryptedMessageDecorator类 :扩展MessageDecorator,通过重写 send 方法添加加密功能。
  5. LoggedMessageDecorator类 :扩展MessageDecorator,通过重写 send 方法添加日志记录功能。

通过这种方式,我们可以在不修改原始类的情况下,为对象动态添加新功能。这提供了一种比继承更灵活、可扩展性更强的方式来增强对象的行为。

类图

装饰器模式(Decorator Pattern)是一种结构型设计模式,允许你通过将对象放入包装对象中来动态地添加行为,相比继承更加灵活。以下是装饰器模式的类图示例:

java 复制代码
+----------------------------------+
|             Component            |
+----------------------------------+
| operation(): void                |
+----------------------------------+
         ^
         |
         |
+-----------------------------+
|         ConcreteComponent   |
+-----------------------------+
| operation(): void           |
+-----------------------------+

        △
        |
+-----------------------------+
|          Decorator           |
+-----------------------------+
| component: Component         |
| operation(): void           |
+-----------------------------+
        |
        |
+-----------------------------+
|       ConcreteDecoratorA    |
+-----------------------------+
| operation(): void           |
| addedBehavior(): void       |
+-----------------------------+

        △
        |
+-----------------------------+
|       ConcreteDecoratorB    |
+-----------------------------+
| operation(): void           |
| addedBehavior(): void       |
+-----------------------------+

说明:

  1. Component(抽象构件):

    • 定义一个对象接口,可以给这些对象动态地添加职责。
    • operation() 是抽象操作,可以是接口或者抽象类。
  2. ConcreteComponent(具体构件):

    • 定义一个具体的对象,也可以给这个对象添加一些职责。
  3. Decorator(装饰器抽象类):

    • 持有一个 Component 对象的引用,并定义一个与 Component 接口一致的接口。
    • 可以用来装饰 Component 对象,增加其行为。
  4. ConcreteDecoratorA、ConcreteDecoratorB(具体装饰器):

    • 负责给具体构件对象添加额外的职责。

类图说明:

  • Component 是抽象构件角色,定义了对象接口。
  • ConcreteComponent 是具体构件角色,实现了 Component 接口的具体对象。
  • Decorator 是装饰器抽象类,持有一个 Component 对象的引用,并定义了与 Component 接口一致的接口。
  • ConcreteDecoratorA、ConcreteDecoratorB 是具体装饰器类,实现了 Decorator 定义的接口,负责给 Component 对象添加额外的职责。

装饰器模式的核心是通过组合而非继承来扩展对象的功能,使得动态添加功能更加灵活。

相关推荐
java资料站3 分钟前
五、Spring AI Alibaba · Memory · Saver(短期会话)
java·spring·microsoft
xh didida6 分钟前
Linux -- 基础IO
linux·服务器·开发语言·c++
个 人 练 习 生25 分钟前
C++ string 类模拟实现:从底层理解字符串(上)
开发语言·c++·经验分享·学习·程序人生
2601_9669496534 分钟前
多市场量化策略的数据接口应该如何设计:从数据层架构到策略接入
开发语言·python·数据分析·pandas·量化交易·股票数据·quantdash
坤坤子吖37 分钟前
C++智能指针:RAII、shared_ptr与内存泄漏
开发语言·c++·笔记·学习
奋斗的阿狸_19861 小时前
ESP32-S3 + ES7210 四通道麦克风录音上传方案
c语言·开发语言·fpga开发
夜之眷属1 小时前
Core dump 崩溃排查:JVM 宕机后,那份 core 文件怎么用 gdb 还原现场
java·运维·服务器·jvm
weixin199701080161 小时前
《1688图片空间API踩坑:img.upload 与 album.* 的防盗链与CDN缓存问题》(附Python源码)
开发语言·python·缓存
波加曼大王1 小时前
# vLLM不要迷信PagedAttention神话,聊聊线上藏着的内部碎片陷阱
java·架构
SL_staff1 小时前
目标健康度自检清单:开发者视角下的目标-计划-任务链路断点诊断
java·开源·github