design-pattern-golang/observer.go at master - GitHub

文章推薦指數: 80 %
投票人數:10人

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.



請為這篇文章評分?