Skip to content

Instantly share code, notes, and snippets.

@longfellowone
Forked from sayden/ObserverDesignPattern.go
Created December 21, 2018 00:36
Show Gist options
  • Save longfellowone/8556c83f467d44fe70e318ce8980aeb8 to your computer and use it in GitHub Desktop.
Save longfellowone/8556c83f467d44fe70e318ce8980aeb8 to your computer and use it in GitHub Desktop.
Observer Pattern in Golang
package main
import "fmt"
//Listener is a
type Listener struct {
ID int
}
//ListenerInterface is an
type ListenerInterface interface {
execute(m string)
}
func (l *Listener) execute(m string) {
fmt.Printf("%q message receiver for id %d \n", m, l.ID)
}
//Subject is an
type Subject struct {
listeners []ListenerInterface
}
//AddListener is a
func (s *Subject) addListener(l ListenerInterface) {
s.listeners = append(s.listeners, l)
}
func (s *Subject) notify(m string) {
for _, l := range s.listeners {
if l != nil {
l.execute(m)
}
}
}
var iter int
func newListener() *Listener {
l := Listener{iter}
iter++
return &l
}
func main() {
iter = 0
s := Subject{listeners: make([]ListenerInterface, 0)}
l := newListener()
s.addListener(l)
for i := 0; i < 5; i++ {
l = newListener()
s.addListener(l)
}
s.notify("Hello")
s.notify("Goobye")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment