Just for FUN--GO implement the Observer pattern

Source: Internet
Author: User
Tags sprintf
This is a creation in Article, where the information may have evolved or changed.

Code

Package Mainimport ("FMT") type Subject interface {registerobserver (o Observer) removeobserver (o Observer) No Tifyallobservers ()}type Observer Interface {//temperature, humidity, barometric Update (temp float32, humidity float32, pressure float32)}ty PE weatherdata struct {temperature float32 humidity float32 pressure float32 observers map[observer]b Ool}func Newweathdata () *weatherdata {return &weatherdata{observers:make (Map[observer]bool),}}func (w D *weatherdata) registerobserver (o Observer) {wd. Observers[o] = True}func (wd *weatherdata) removeobserver (o Observer) {if _, OK: = WD. Observers[o]; OK {Delete (wd). Observers, O)}}func (wd *weatherdata) Notifyallobservers () {for O, _: = Range WD. Observers {O.update (WD. Temperature, WD. Humidity, WD. pressure)}}func (wd *weatherdata) setmeasurements (temp float32, humidity float32, pressure float32) {wd. Temperature = temp WD. Humidity = Humidity WD.  pressure = pressure  Wd. Notifyallobservers ()}type currentconditionsdisplay struct {temperature float32 humidity float32 Weathdata S Ubject}func Newcurrentconditionsdisplay (weathdata Subject) *currentconditionsdisplay {CCD: = & currentconditionsdisplay{Weathdata:weathdata,} weathdata.registerobserver (CCD) return Ccd}func (CCD *Cu Rrentconditionsdisplay) Update (temp float32, humidity float32, pressure float32) {CCD. Temperature = temp CCD. Humidity = humidity//pressure no use to CCD. Display ()}func (CCD *currentconditionsdisplay) display () {FMT. PRINTLN ("Current conditions:" + FMT. Sprintf ("%v", CCD. Temperature) + "F degrees and" + FMT. Sprintf ("%v", CCD. Humidity) + "% humidity")}func main () {weathdata: = Newweathdata () _ = Newcurrentconditionsdisplay (Weathdata) we Athdata.setmeasurements (30.4, weathdata.setmeasurements, 29.2) weathdata.setmeasurements (78, 90, 29.2 )}

Test

Output

Current conditions: 80F degrees and 65% humidityCurrent conditions: 82F degrees and 70% humidityCurrent conditions: 78F degrees and 90% humidity
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.