桥接模式通过组合解耦抽象与实现,如通知系统中类型与渠道独立扩展。示例中Notification和EmergencyNotification对接NotificationSender接口,EmailSender、SMSSender实现发送方式,新增类型或渠道无需修改原有代码,符合开闭原则,避免类爆炸,提升系统可维护性。

在 Go 语言中,桥接模式(Bridge Pattern)是一种结构型设计模式,用于将抽象部分与实现部分分离,使它们可以独立变化。这种模式特别适用于存在多个维度扩展的场景,比如不同类型的对象和不同的实现方式组合时,避免类爆炸问题。
桥接模式的核心思想
桥接模式通过组合而非继承来连接抽象层和实现层。它把变化的两个维度(如类型和实现)解耦,各自独立演化。
典型结构包括:
- Abstraction(抽象类):定义高层控制逻辑,持有一个对实现接口的引用
- Implementor(实现接口):定义实现层的接口,通常由具体实现类完成
- Refined Abstraction(扩展抽象类):可选,对抽象接口进行扩展
- Concrete Implementor(具体实现):实现 Implementor 接口的具体行为
实际示例:消息通知系统解耦
假设我们正在开发一个通知系统,需要支持多种通知类型(如普通通知、紧急通知),同时支持多种发送渠道(邮件、短信、钉钉)。如果用继承,很容易导致类数量爆炸。使用桥接模式可以清晰解耦。
立即学习“go语言免费学习笔记(深入)”;
<span style="color:blue;">package</span> main
<span style="color:blue;">import</span> (
<span style="color:darkgreen;">"fmt"</span>
)
<span style="color:gray;">// NotificationSender 实现接口:定义发送方式</span>
<span style="color:blue;">type</span> NotificationSender <span style="color:blue;">interface</span> {
Send(message <span style="color:blue;">string</span>) <span style="color:blue;">string</span>
}
<span style="color:gray;">// EmailSender 具体实现</span>
<span style="color:blue;">type</span> EmailSender <span style="color:blue;">struct</span> {}
<span style="color:blue;">func</span> (e *EmailSender) Send(message <span style="color:blue;">string</span>) <span style="color:blue;">string</span> {
<span style="color:blue;">return</span> fmt.Sprintf(<span style="color:darkgreen;">"通过邮件发送: %s"</span>, message)
}
<span style="color:gray;">// SMSSender 具体实现</span>
<span style="color:blue;">type</span> SMSSender <span style="color:blue;">struct</span> {}
<span style="color:blue;">func</span> (s *SMSSender) Send(message <span style="color:blue;">string</span>) <span style="color:blue;">string</span> {
<span style="color:blue;">return</span> fmt.Sprintf(<span style="color:darkgreen;">"通过短信发送: %s"</span>, message)
}
<span style="color:gray;">// Notification 抽象层:定义通知类型</span>
<span style="color:blue;">type</span> Notification <span style="color:blue;">struct</span> {
sender NotificationSender
}
<span style="color:blue;">func</span> NewNotification(sender NotificationSender) *Notification {
<span style="color:blue;">return</span> &Notification{sender: sender}
}
<span style="color:blue;">func</span> (n *Notification) Notify() <span style="color:blue;">string</span> {
<span style="color:blue;">return</span> n.sender.Send(<span style="color:darkgreen;">"您有一条新通知"</span>)
}
<span style="color:gray;">// EmergencyNotification 扩展抽象:紧急通知</span>
<span style="color:blue;">type</span> EmergencyNotification <span style="color:blue;">struct</span> {
sender NotificationSender
}
<span style="color:blue;">func</span> NewEmergencyNotification(sender NotificationSender) *EmergencyNotification {
<span style="color:blue;">return</span> &EmergencyNotification{sender: sender}
}
<span style="color:blue;">func</span> (e *EmergencyNotification) Notify() <span style="color:blue;">string</span> {
<span style="color:blue;">return</span> e.sender.Send(<span style="color:darkgreen;">"【紧急】系统告警!"</span>)
}
<span style="color:blue;">func</span> main() {
emailSender := &EmailSender{}
smsSender := &SMSSender{}
normalViaEmail := NewNotification(emailSender)
emergencyViaSMS := NewEmergencyNotification(smsSender)
fmt.Println(normalViaEmail.Notify())
fmt.Println(emergencyViaSMS.Notify())
}
输出结果说明
运行上述代码会得到:
- 通过邮件发送: 您有一条新通知
- 通过短信发送: 【紧急】系统告警!
可以看到,通知类型和发送渠道完全解耦。新增一种发送方式(如钉钉)或一种通知类型(如营销通知),只需添加对应结构体并实现接口,无需修改已有代码。
桥接模式的优势与适用场景
使用桥接模式后,系统具备更好的扩展性和维护性:
- 抽象和实现可以独立变化,符合开闭原则
- 避免多层继承导致的类爆炸
- 运行时可以动态切换实现
适合用于框架设计、组件化系统、多平台适配等需要高度解耦的场景。
基本上就这些。Go 通过接口和组合天然支持桥接模式,不需要复杂的继承体系也能实现灵活的设计。










