装饰器模式是结构型设计模式,通过接口和组合在Golang中动态扩展对象功能而不修改原代码;示例用MessageSender接口实现日志与加密装饰器,并支持嵌套组合。

装饰器模式是一种结构型设计模式,允许在不修改原有对象代码的前提下,动态地为对象添加新功能。在 Golang 中虽然没有类和继承机制,但通过接口和组合的方式,可以非常自然地实现装饰器模式。
要实现装饰器模式,第一步是定义一个统一的接口,表示被装饰的对象所共有的行为。
示例场景: 假设我们有一个消息发送器,支持发送普通文本消息,现在想动态添加日志记录、加密等额外功能。先定义接口:
type MessageSender interface {
Send(msg string) error
}
实现基础的消息发送器:
立即学习“go语言免费学习笔记(深入)”;
type EmailSender struct{}
func (e *EmailSender) Send(msg string) error {
fmt.Printf("发送邮件: %s\n", msg)
return nil
}
装饰器本身也实现相同的接口,并持有一个 MessageSender 类型的字段。这样可以在调用前后插入额外逻辑。
例如,实现一个日志装饰器:
type LoggingDecorator struct {
sender MessageSender
}
func NewLoggingDecorator(sender MessageSender) *LoggingDecorator {
return &LoggingDecorator{sender: sender}
}
func (l *LoggingDecorator) Send(msg string) error {
fmt.Printf("[日志] 即将发送消息: %s\n", msg)
err := l.sender.Send(msg)
if err != nil {
fmt.Printf("[日志] 发送失败: %v\n", err)
} else {
fmt.Printf("[日志] 发送成功\n")
}
return err
}
再实现一个加密装饰器:
type EncryptDecorator struct {
sender MessageSender
}
func NewEncryptDecorator(sender MessageSender) *EncryptDecorator {
return &EncryptDecorator{sender: sender}
}
func (e *EncryptDecorator) Send(msg string) error {
encrypted := base64.StdEncoding.EncodeToString([]byte(msg))
fmt.Printf("[加密] 消息已编码: %s\n", encrypted)
return e.sender.Send(encrypted)
}
装饰器模式的强大之处在于可以层层嵌套,灵活组合功能。
例如,我们希望先加密再记录日志:
func main() {
sender := &EmailSender{}
// 先加日志,再加加密
decorated := NewLoggingDecorator(
NewEncryptDecorator(sender),
)
decorated.Send("Hello, 世界")
}
输出结果:
[日志] 即将发送消息: Hello, 世界 [加密] 消息已编码: SGVsbG8sIOS4lueVjA== 发送邮件: SGVsbG8sIOS4lueVjA== [日志] 发送成功
注意装饰顺序会影响执行流程。如果交换装饰器顺序,日志中看到的就是原始消息。
基本上就这些。Golang 的接口和组合机制让装饰器模式实现简洁且类型安全,无需反射也能做到高度解耦。关键是保持接口小而明确,每个装饰器只做一件事。
以上就是如何使用Golang实现装饰器模式增强功能_Golang装饰器模式应用实例的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号