douyun1972
2019-07-14 11:47 阅读 98
已采纳

实现观察者设计模式的Go方法

My experience is with OOP languages but I've started trying out Go. I'm having trouble working out the best way to implement the Observer design pattern in Go.

I've organised my project as follows where everything in the observers folder is part of package observers and everything in the subjects folder is part of package subjects. The attaching of observers to subjects is done in main.go.

my-project/
  main.go
  observers/
    observer.go
    observer_one.go
    observer_two.go
  subjects/
    subject.go
    subject_one.go

I've seen this section in the Go wiki about interfaces:

Go interfaces generally belong in the package that uses values of the interface type, not the package that implements those values. The implementing package should return concrete (usually pointer or struct) types: that way, new methods can be added to implementations without requiring extensive refactoring.

Keeping the comment from the Go Wiki in mind. I've implemented like this (left out function implementations):

subject.go:

type ObserverInterface interface {
    Update(subject *Subject, updateType string)
}

type Subject struct {
    observers map[string][]ObserverInterface
}

func (s *Subject) Attach(observer ObserverInterface, updateType string) {}

func (s *Subject) Detach(observer ObserverInterface, updateType string) {}

func (s *Subject) notify(updateType string) {}

observer.go:

type SubjectInterface interface {
   Attach(observer Observer, updateType string)
   Detach(observer Observer, updateType string)
   notify(updateType string)
}

type Observer struct {
    uuid uuid.UUID
}

observer_one.go

type ObserverOne struct {
    Observer
}

func (o *ObserverOne) Update(subject *SubjectInterface, updateType string) {}

main.go

subjectOne := &SubjectOne{}
observerOne := &ObserverOne{Observer{uuid: uuid.New()}}
subjectOne.Attach(observerOne, "update_type")

I expect to be able to use SubjectInterface for the argument to the Update() method in ObserverOne so that I can avoid having dependencies between my subject package and my observer package but I get the following compile-time error.

observers/observer_one.go:29:35: cannot use &observer (type *ObserverOne) as type subjects.ObserverInterface in argument to SubjectOne.Subject.Attach:
    *ObserverOne does not implement subjects.ObserverInterface (wrong type for Update method)
        have Update(*SubjectInterface, string)
        want Update(*subjects.Subject, string)

If I replace the definition of Update() in observer_one.go with the following it compiles fine but i thought the idea was to decouple the packages using interfaces:

func (o *ObserverOne) Update(subject *subjects.Subject, updateType string) {}
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    duanchan9354 duanchan9354 2019-07-14 12:02

    First, don't use pointers to interfaces.

    func (o *ObserverOne) Update(subject *SubjectInterface, updateType string) {}
    

    should be

    func (o *ObserverOne) Update(subject SubjectInterface, updateType string) {}
    

    Second, you've defined your interface to require a concrete type:

    type ObserverInterface interface {
        Update(subject *Subject, updateType string)
    }
    

    Instead, make it accept an interface:

    type ObserverInterface interface {
        Update(subject SubjectInterface, updateType string)
    }
    
    点赞 评论 复制链接分享
  • dqczgtem06898 dqczgtem06898 2019-07-14 12:29

    Flimzy solution still ties things together with explicit interfaces into the Observers vertical abstraction (ie: ObserverInterface).

    a more generic version may look like this:

    package main
    
    import (
        "fmt"
    )
    
    func main() {
    
        obs := Observer{SubjectObserver{}}
    
        s := Subject{Observer: obs}
        s.Update()
    
    }
    
    type Subject struct {
        Observer
        UUID string
    }
    
    func (s *Subject) Update() {
        s.Observer.Notify(s)
    }
    
    type SubjectObserver struct{}
    
    func (s SubjectObserver) Notify(v interface{}) {
        x, ok := v.(*Subject)
        if ok {
            fmt.Printf("do whatever with %#v
    ", x)
        }
    }
    
    type Observer []ObserverOf
    
    func (o Observer) Notify(s interface{}) {
        for _, ob := range o {
            ob.Notify(s)
        }
    }
    
    type ObserverOf interface {
        Notify(interface{})
    }
    

    That the PO embeds the observer is a detail.

    点赞 评论 复制链接分享

相关推荐