优化扇出观察者示例的逻辑
This commit is contained in:
parent
a61d49c8c3
commit
310206ad68
@ -17,16 +17,16 @@ func NewSubject() Subject {
|
||||
}
|
||||
|
||||
// Attach 观察者绑定
|
||||
func (s *Subject) Attach(observer ...Observer) {
|
||||
s.observers = append(s.observers, observer...)
|
||||
func (s *Subject) Attach(obs ...Observer) {
|
||||
s.observers = append(s.observers, obs...)
|
||||
}
|
||||
|
||||
// Detach 观察者解绑
|
||||
func (s *Subject) Detach(observer Observer) {
|
||||
for i, obs := range s.observers {
|
||||
if obs == observer {
|
||||
func (s *Subject) Detach(obs Observer) {
|
||||
for i, o := range s.observers {
|
||||
if o == obs {
|
||||
s.observers = append(s.observers[:i], s.observers[i+1:]...)
|
||||
close(observer)
|
||||
close(obs)
|
||||
break
|
||||
}
|
||||
}
|
||||
@ -34,18 +34,17 @@ func (s *Subject) Detach(observer Observer) {
|
||||
|
||||
// Notify 通知观察者
|
||||
func (s *Subject) Notify(data any) {
|
||||
// 这里可考虑不需要使用协程运行
|
||||
go s.fanOut(s.in, s.observers)
|
||||
s.in <- data
|
||||
}
|
||||
|
||||
// fanOut 扇出模式实现
|
||||
func (s *Subject) fanOut(ch <-chan interface{}, out []Observer) {
|
||||
var cases []reflect.SelectCase
|
||||
// 添加输入 chan 的 reflect.SelectCase
|
||||
cases = append(cases, reflect.SelectCase{
|
||||
Dir: reflect.SelectRecv,
|
||||
Chan: reflect.ValueOf(ch),
|
||||
})
|
||||
// 绑定输入 chan 的 reflect.SelectCase
|
||||
cases := []reflect.SelectCase{
|
||||
{Dir: reflect.SelectRecv, Chan: reflect.ValueOf(ch)},
|
||||
}
|
||||
|
||||
go func() {
|
||||
defer func() {
|
||||
|
@ -29,5 +29,6 @@ func TestObserver(t *testing.T) {
|
||||
newUser2 := "gan"
|
||||
userRegister.Notify(newUser1)
|
||||
userRegister.Notify(newUser2)
|
||||
// 休眠防止主协程退出
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user