go-pattern-examples/behavior/03_observer/obserser.go

59 lines
1.3 KiB
Go
Raw Permalink Normal View History

2020-04-21 17:50:21 +03:00
package observer
2020-05-02 16:23:04 +03:00
import (
"context"
"fmt"
)
2020-04-21 17:50:21 +03:00
2020-05-02 16:23:04 +03:00
//使用RSS邮件订阅的例子
//某科技论坛更新时候,通过邮件通知订阅该版块的用户关注更新.
type updates struct {
topic string
order int
2020-04-21 17:50:21 +03:00
}
2020-05-02 16:23:04 +03:00
//TechBBS 科技论坛,是发送通知的主体
type TechBBS struct {
mailObservers []IMailReceiver ///邮件订阅该板块动态的用户
context context.Context ///更新的上下消息,其实就是一堆参数了
2020-04-21 17:50:21 +03:00
}
2020-05-02 16:23:04 +03:00
//IMailReceiver 邮件接收者就是订阅更新的用户
type IMailReceiver interface {
Notice(context.Context)
2020-04-21 17:50:21 +03:00
}
2020-05-02 16:23:04 +03:00
//Registry 提供给用户通过邮件注册获取/订阅更新动态的能力
func (t *TechBBS) Registry(receiver IMailReceiver) {
t.mailObservers = append(t.mailObservers, receiver)
2020-04-21 17:50:21 +03:00
}
2020-05-02 16:23:04 +03:00
//SetConext 设置更新内容的上下文
func (t *TechBBS) SetConext(ctx context.Context) {
t.context = ctx
2020-04-21 17:50:21 +03:00
}
2020-05-02 16:23:04 +03:00
//notifyUpdate 通知订阅用户,我更新了,你们可以来看了
func (t *TechBBS) noticeAllUpdate() {
for _, m := range t.mailObservers {
//逐个通知
m.Notice(t.context)
}
2020-04-21 17:50:21 +03:00
}
2020-05-02 16:23:04 +03:00
//User 用户
type User struct {
2020-04-21 17:50:21 +03:00
name string
}
2020-05-02 16:23:04 +03:00
//Notice 用户收到订阅通知
func (u *User) Notice(ctx context.Context) {
content := ctx.Value(updates{}).(updates)
fmt.Printf("%s receive updates notice\n", u.name)
2020-04-21 17:50:21 +03:00
2020-05-02 16:23:04 +03:00
fmt.Printf("updates order: %d content%s\n", content.order, content.topic)
2020-04-21 17:50:21 +03:00
}