design-pattern-golang/observer.go at master - GitHub
文章推薦指數: 80 %
package observer. import "fmt". type Subject struct {. observers []Observer. context string. } func NewSubject() *Subject {. return &Subject{. Skiptocontent {{message}} tonydeng / design-pattern-golang Public Notifications Star 9 Fork 1 Code Issues 0 Pullrequests 0 Actions Projects 0 Wiki Security Insights More Code Issues Pullrequests Actions Projects Wiki Security Insights Permalink master Branches Tags Couldnotloadbranches Nothingtoshow Loading {{refName}} default Couldnotloadtags Nothingtoshow {{refName}} default Loading design-pattern-golang/observer/observer.go / Jumpto Subject Function NewSubject Function Attach Method notify Method UpdateContext Method Observer Function Reader Function NewReader Function Update Method Gotofile Gotofile T Gotoline L Gotodefinition R Copypath Copypermalink Cannotretrievecontributorsatthistime 47lines(37sloc) 679Bytes Raw Blame OpenwithDesktop Viewraw Viewblame ThisfilecontainsbidirectionalUnicodetextthatmaybeinterpretedorcompileddifferentlythanwhatappearsbelow.Toreview,openthefileinaneditorthatrevealshiddenUnicodecharacters. LearnmoreaboutbidirectionalUnicodecharacters Showhiddencharacters packageobserver import"fmt" typeSubjectstruct{ observers[]Observer contextstring } funcNewSubject()*Subject{ return&Subject{ observers:make([]Observer,0), } } func(s*Subject)Attach(oObserver){ s.observers=append(s.observers,o) } func(s*Subject)notify(){ for_,o:=ranges.observers{ o.Update(s) } } func(s*Subject)UpdateContext(contextstring){ s.context=context s.notify() } typeObserverinterface{ Update(*Subject) } typeReaderstruct{ namestring } funcNewReader(namestring)*Reader{ return&Reader{ name:name, } } func(r*Reader)Update(s*Subject){ fmt.Printf("%sreceive%s\n",r.name,s.context) } Copylines Copypermalink Viewgitblame Referenceinnewissue Go Youcan’tperformthatactionatthistime. Yousignedinwithanothertaborwindow.Reloadtorefreshyoursession. Yousignedoutinanothertaborwindow.Reloadtorefreshyoursession.
延伸文章資訊
- 1Reacting to File Changes Using the Observer Design Pattern ...
To illustrate the observer pattern in Go, we are going to watch for changes in a local folder. Ev...
- 2Observer pattern in Go language - Stack Overflow
The Go way to implement the Observer design pattern - Stack ...
- 3design-pattern-golang/observer.go at master - GitHub
package observer. import "fmt". type Subject struct {. observers []Observer. context string. } fu...
- 4The Observer Design Pattern In Go - Morioh
The Observer design pattern is a fundamental tool for any capable software engineer. In one sente...
- 5Generator Pattern and Observer Pattern - Part Two - YouTube
tensorprogramming #golang #designpatternsIn this tutorial, we take a look at the Generator patter...