You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

44 lines
894 B

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package observer_mode
import "container/list"
// 观察者管理中心subject
type Subject struct {
Observers *list.List
params interface{}
}
//注册观察者角色
func (s *Subject) Attach(observe ObserverInterface) {
s.Observers.PushBack(observe)
}
//删除观察者角色
func (s *Subject) Detach(observer ObserverInterface) {
for ob := s.Observers.Front(); ob != nil; ob = ob.Next() {
if ob.Value.(*ObserverInterface) == &observer {
s.Observers.Remove(ob)
break
}
}
}
//通知所有观察者
func (s *Subject) Notify() {
var l_temp *list.List = list.New()
for ob := s.Observers.Front(); ob != nil; ob = ob.Next() {
l_temp.PushBack(ob.Value)
ob.Value.(ObserverInterface).Update(s)
}
s.Observers = l_temp
}
func (s *Subject) BroadCast(args ...interface{}) {
s.params = args
s.Notify()
}
func (s *Subject) GetParams() interface{} {
return s.params
}