观察者模式通过主题维护观察者列表并在状态变化时通知,发布订阅模式利用channel和EventBus解耦发布与订阅;两者均适用于消息广播、状态同步等场景。

观察者模式和发布订阅模式在事件驱动系统中非常常见,Golang 虽然没有内置的事件机制,但通过接口、channel 和 goroutine 可以轻松实现这两种模式。它们常用于解耦模块、实现消息广播、状态同步等场景。
观察者模式定义了一种一对多的依赖关系,当一个对象的状态发生变化时,所有依赖它的对象都会收到通知。
核心结构包含两个角色:主题(Subject)和观察者(Observer)。主题维护观察者列表,并在状态变化时通知它们。
示例代码:
立即学习“go语言免费学习笔记(深入)”;
type Observer interface {
Update(message string)
}
<p>type Subject struct {
observers []Observer
state string
}</p><p>func (s *Subject) Attach(o Observer) {
s.observers = append(s.observers, o)
}</p><p>func (s *Subject) Notify() {
for _, o := range s.observers {
o.Update(s.state)
}
}</p><p>func (s *Subject) SetState(state string) {
s.state = state
s.Notify()
}</p><p>type ConcreteObserver struct {
name string
}</p><p>func (co *ConcreteObserver) Update(message string) {
fmt.Printf("[%s] 收到通知: %s\n", co.name, message)
}
使用方式:创建主题,注册多个观察者,调用 SetState 后所有观察者自动更新。
发布订阅模式更进一步,通过中间的“事件总线”解耦发布者和订阅者。Golang 的 channel 非常适合这种异步通信。
与观察者模式不同,发布者不直接维护订阅者列表,而是将消息发送到某个 topic 的 channel 中,由调度器转发给订阅者。
实现思路:
type EventBus struct {
subscribers map[string][]chan string
mutex sync.RWMutex
}
<p>func NewEventBus() *EventBus {
return &EventBus{
subscribers: make(map[string][]chan string),
}
}</p><p>func (eb *EventBus) Subscribe(topic string) <-chan string {
ch := make(chan string, 10) // 缓冲 channel 避免阻塞
eb.mutex.Lock()
eb.subscribers[topic] = append(eb.subscribers[topic], ch)
eb.mutex.Unlock()
return ch
}</p><p>func (eb *EventBus) Publish(topic, message string) {
eb.mutex.RLock()
subs := eb.subscribers[topic]
eb.mutex.RUnlock()</p><pre class="brush:php;toolbar:false;"><pre class="brush:php;toolbar:false;">for _, ch := range subs {
select {
case ch <- message:
default:
// 防止因消费者慢导致阻塞
}
}}
这类模式广泛应用于日志系统、配置热更新、微服务通信、UI 状态同步等。
技巧一:使用 goroutine 异步通知
避免在主线程中同步调用所有观察者,影响性能。可以为每个通知启动 goroutine:
for _, o := range s.observers {
go o.Update(s.state) // 异步执行
}
技巧二:支持 topic 匹配(通配符)
高级消息系统常支持模糊订阅,如 "user.*" 匹配 "user.create" 和 "user.delete"。可通过正则或前缀匹配扩展 EventBus。
技巧三:资源清理
长时间运行的服务需注意 channel 泄漏。可提供 Unsubscribe 方法,并使用 context 控制生命周期:
func (eb *EventBus) Unsubscribe(topic string, ch <-chan string) {
eb.mutex.Lock()
defer eb.mutex.Unlock()
// 移除对应 channel
}
技巧四:结合 context 实现超时控制
在发布消息时加入 context,防止某些订阅者处理过慢拖累整体:
ctx, cancel := context.WithTimeout(context.Background(), 100*time.Millisecond)
defer cancel()
<p>select {
case ch <- message:
case <-ctx.Done():
// 跳过慢速订阅者
}
基本上就这些。观察者模式适合对象间紧耦合的通知,发布订阅更适合跨模块、异步、松耦合的通信。Golang 凭借 channel 和并发模型,实现起来简洁高效。关键是根据业务选择合适粒度的 topic 和合理的缓冲策略,避免阻塞和泄漏。
以上就是如何用Golang实现观察者与发布订阅模式_Golang 发布订阅模式应用技巧的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号