观察者模式Go语言实现

本文介绍了观察者模式,一种用于定义订阅机制的设计模式,当对象状态变化时通知观察者。通过商店与顾客的例子展示了其工作原理。类图中包括抽象主题、具体发布者、订阅者和具体订阅者四个角色。代码示例展示了如何在Go语言中实现观察者模式,包括订阅、取消订阅和通知功能。最后总结了模式优缺点,强调了其松耦合和可扩展性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

简介

观察者模式是一种行为设计模式,目的在于定义一种订阅机制,在对象事件发生时通知“观察”该对象的对象。例如:商店和顾客,顾客要购买新款手机但未到货,顾客在商店进行了订阅,只要商店新款手机到货,就立即发送短信通知顾客。这里顾客是观察者(Observer),商店被观察者(subject)。

类图模式

对应类图可用如下表示:
典型观察者类图

该设计模式共有四类角色:
1、抽象主题(Subject):该角色可以增加或者删除观察者对象。
2、具体发布者(ConcreteSubject):会向其他Observer对象发送值得关注的事件(自身状态发生改变)。发布者中包含一个允许新订阅者加入和当前订阅者离开list的订阅框架。
3、订阅者(Observer):该接口声明了通知接口。该接口仅包含一个 update 更新方法。该方法可以拥有多个参数,使发布者能在更新时传递事件的详细信息。
4、具体订阅者(Observer1…ObserverN): 实现了订阅者接口的类,都需要实现update方法,具体发布者可以调用这些Observer的update方法进行更新。

实例代码

package main

import "fmt"

// The customer interface
type interfaceCustomer interface {
    Update(phoneName string)
}

// The class of customer
type CustomerALL struct {
    phoneName string
}

func (C *CustomerALL) Update(phoneName string) {
    fmt.Println(C.phoneName, "-Get the Phone", phoneName)
}

// The market interface
type interfaceMarket interface {
    Attach(customer interfaceCustomer)
    Dettach(customer interfaceCustomer)
    NotifyObservers(phoneName string)
}

// The concrete class of market
type MarketA struct {
    list []interfaceCustomer
}

func (m *MarketA) Attach(customer interfaceCustomer) {
    // Add some customers
    m.list = append(m.list, customer)
}

func (m *MarketA) Dettach(customer interfaceCustomer) {
    // Delete customer
    for i, value := range m.list {
        if value == customer {
            //delete the customer
            m.list = append(m.list[:i], m.list[i+1:]...)
        }
    }
}

func (m *MarketA) NotifyObservers(phoneName string) {
    // Notify all customers that the market have the phone now!
    for _, customer := range m.list {
        customer.Update(phoneName)
    }
}

func (m *MarketA) Change (phoneName string) {
    m.NotifyObservers(phoneName)
}

func main() {
    // create a Market
    market := MarketA{list: []interfaceCustomer{}}
    // create customerA
    customerA := CustomerALL{phoneName:"A"}
    // customerA subscribe the notify from Market
    market.Attach(&customerA)
    // create customerB
    customerB := CustomerALL{phoneName:"B"}
    // customerB subscribe the notify from Market
    market.Attach(&customerB)
    // The market have new phone  now
    market.Change("New Phone")
    fmt.Println("Customer B fire the subscribe!")
    market.Dettach(&customerB)
    market.Change("New Phone Plus")
}

运行结果如下所示:

A -Get the Phone New Phone
B -Get the Phone New Phone
Customer B fire the subscribe!
A -Get the Phone New Phone Plus

总结

该模式的优点:被观察者和观察者之间实现了抽象耦合,容易扩展。广播模式,只要订阅,就能收到消息。
该模式的缺点:观察者收到变化,不知道什么时候发生的变化,轮询通知观察者,比较耗时。

### Go语言中的观察者模式Go语言实现观察者模式可以通过定义接口来解耦被观察对象和多个观察者之间的依赖关系。这种方式使得当一个对象的状态发生变化时,所有依赖于它的对象都会得到通知并自动更新。 #### 定义观察者的接口 为了使代码更加灵活且易于维护,在Go中通常会先定义一个`Observer`接口,这个接口包含了接收通知的方法签名: ```go type Observer interface { Update(message string) } ``` 此方法用于向所有的注册过的观察者发送状态改变的信息[^1]。 #### 创建具体的观察者类 接着可以基于上述接口创建一些具体的观察者实例,这些实例实现了`Update()`函数以便处理来自主题的通知: ```go // ConcreteObserverA 是一种类型的观察者 type ConcreteObserverA struct{} func (o *ConcreteObserverA) Update(message string) { fmt.Printf("ConcreteObserverA received message: %s\n", message) } // ConcreteObserverB 另外一种类型的观察者 type ConcreteObserverB struct{} func (o *ConcreteObserverB) Update(message string) { fmt.Printf("ConcreteObserverB received message: %s\n", message) } ``` 以上两个结构体分别代表两种不同类型的观察者,并各自有不同的响应逻辑[^2]。 #### 设计Subject(被观察者) 之后需要构建一个主体(`Subject`),它负责管理一组观察者列表以及提供附加或移除观察者的能力;同时也要有办法去通知它们有关自身的更改情况: ```go type Subject struct { observers []Observer } func NewSubject() *Subject { return &Subject{observers: make([]Observer, 0)} } func (s *Subject) Attach(observer Observer) { s.observers = append(s.observers, observer) } func (s *Subject) Detach(observer Observer) { for i := range s.observers { if s.observers[i] == observer { s.observers = append(s.observers[:i], s.observers[i+1:]...) break } } } func (s *Subject) Notify(message string) { for _, obs := range s.observers { go obs.Update(message) // 使用goroutine并发执行以提高效率 } } ``` 这里特别注意的是`Notify()`方法内部采用了协程的方式调用了各个观察者的`Update()`方法,从而允许异步地完成通知过程,提高了程序性能[^3]。 #### 测试观察者模式的功能 最后编写一段简单的测试代码验证整个机制的工作原理: ```go func main() { subject := NewSubject() var ( observerA = new(ConcreteObserverA) observerB = new(ConcreteObserverB) ) subject.Attach(observerA) subject.Attach(observerB) time.AfterFunc(time.Second*2, func() { // 模拟两秒后的事件触发 subject.Notify("State has changed!") }) select {} // 阻塞主线程防止过早退出 } ``` 这段代码展示了如何创建一个新的`Subject`实例并将几个不同的观察者添加进去。经过一段时间延迟后模拟了一次状态变更的发生,这时所有已登记的观察者都将接收到相应的消息提示。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值