7

Go中用Goroutine实现观察者模式

 8 months ago
source link: https://www.jdon.com/71836.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Go中用Goroutine实现观察者模式 - 极道

观察者是模式事件驱动编程中的主要内容,其中对象(称为“主题”)维护其依赖者(观察者)的列表,并通知它们任何状态更改。在 Python等语言中,信号为此类解耦组件提供了一种通信机制。但是我们如何利用 Go的原并发语(例如 goroutine)来实现这一目标呢?让我们深入研究观察者模式与 goroutine 的创新结合。

为什么用 Goroutine 来实现观察者模式?
Goroutine 提供轻量级并发执行,使其成为处理动态添加或删除观察者以及广事件播而无需停止主线程的完美候选者。

传统上,观察者模式涉及:

  • 主题:维护观察者列表,方便添加或删除观察者,并通知他们变化。
  • 观察者:对主题发送的通知做出反应。

第 1 步:定义主题

type Subject struct {
    observers []chan string
    mu        sync.Mutex
}

func (s *Subject) AddObserver(obs chan string) {
    s.mu.Lock()
    s.observers = append(s.observers, obs)
    s.mu.Unlock()
}

func (s *Subject) RemoveObserver(obs chan string) {
    s.mu.Lock()
    defer s.mu.Unlock()
    for i, observer := range s.observers {
        if obs == observer {
            s.observers = append(s.observers[:i], s.observers[i+1:]...)
            return
        }
    }
}

func (s *Subject) NotifyObservers(message string) {
    s.mu.Lock()
    defer s.mu.Unlock()
    for _, observer := range s.observers {
        go func(ch chan string) {
            ch <- message
        }(observer)
    }
}

在这里,我们在 NotifyObservers 中使用了 goroutines 来同时向所有观察者发送。消息

步骤 2:创建和观察信号
利用 goroutines,我们将创建一个类似信号的观察器:

func main() {
    subject := &Subject{}

// 第一观察员
    obs1 := make(chan string)
    subject.AddObserver(obs1)
    go func() {
        for message := range obs1 {
            fmt.Println("Observer 1 received:", message)
        }
    }()

// 第二观察员
    obs2 := make(chan string)
    subject.AddObserver(obs2)
    go func() {
        for message := range obs2 {
            fmt.Println("Observer 2 received:", message)
        }
    }()

// 广播信息
    subject.NotifyObservers("Hello, Observers!")

// Clean up (in a real-world scenario, handle with care!)
    close(obs1)
    close(obs2)
}

运行上述程序时,两个观察者将同时接收和处理消息。

处理高级场景
通过我们的基本设置,您可以

  • 引入更细粒度的通知:不使用单一的消息类型,而是使用不同的消息(使用结构体或不同的通道),允许观察者监听特定事件。
  • 实施缓冲:使用缓冲通道,您可以发送多条消息,而无需等待接收者准备就绪,从而提供更顺畅的流程。
  • 超时和:上下文使用上下文包,为观察者通知引入超时,确保停滞的观察者不会影响系统。

总结
Goroutines 凭借其轻量级并发模型,为观察者模式提供了一种令人耳目一新的方法,近似于其他语言中的信号机制。通过将设计模式与并发基元相融合,Go 展示了它在构建高效、解耦和事件驱动系统方面的多功能性。随着 Go 在软件领域的不断崛起,这些创新巩固了它作为一种既能满足传统编程范式又能满足现代编程范式的语言的地位。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK