设计模式——观察者模式

2020-11-07  本文已影响0人  DevilRoshan

什么是观察者模式?

又叫发布-订阅模式(Publish/Subscribe),定义对象间一种一对多的依赖关系,使得每当一个对象改变状态,则所有依赖于它的对象都会得到通知并自动更新。

实现

// 事件
type Event struct {
    Data int
}

type Observer interface {
    NotifyCallback(event Event)
}

type Subject interface {
    AddListener(observer Observer)
    RemoveListener(observer Observer)
    Notify(event Event)
}

type EventObserver struct {
    ID   int
    Time time.Time
}

type EventSubject struct {
    Observers sync.Map
}

func (this EventObserver) NotifyCallback(event Event) {
    fmt.Printf("Received:%d after %v\n", event.Data, time.Since(this.Time))
}

func (this *EventSubject) AddListener(observer Observer) {
    this.Observers.Store(observer, struct{}{})
}

func (this *EventSubject) RemoveListener(observer Observer) {
    this.Observers.Delete(observer)
}

func (this *EventSubject) Notify(event Event) {
    this.Observers.Range(func(key, value interface{}) bool {
        if key == nil {
            return false
        }
        key.(Observer).NotifyCallback(event)
        return true
    })
}

func Fib(n int) chan int {
    out := make(chan int)
    go func() {
        defer close(out)
        for i, j := 0, 1; i < n; i, j = i+j, i {
            out <- i
        }
    }()
    return out
}
func TestFib(t *testing.T) {
    //for x := range Fib(10) {
    //  fmt.Println(x)
    //}

    event := EventSubject{Observers:sync.Map{}}
    obs1 := EventObserver{ID:1, Time:time.Now()}
    obs2 := EventObserver{ID:2, Time:time.Now()}

    event.AddListener(obs1)
    event.AddListener(obs2)
    event.RemoveListener(obs1)

    for x:= range Fib(10) {
        event.Notify(Event{Data:x})
    }
}
/*
=== RUN   TestFib
Received:0 after 0s
Received:1 after 0s
Received:1 after 0s
Received:2 after 0s
Received:3 after 0s
Received:5 after 0s
Received:8 after 0s
--- PASS: TestFib (0.00s)
PASS
*/

优点

缺点

使用场景

注意

上一篇下一篇

猜你喜欢

热点阅读