Photo by tito pixel on Unsplash

Observer Pattern in Go

This is the second chapter of Head First Design Patterns book. Though the book has code snippets in Java, my attempt here is to implement the problem discussed in Go. Without further ado, let’s understand the use-case.

The problem statement

We are told to design a weather monitoring application. It consists of 3 sections

  1. Weather data : tracks data from the weather station and forwards it to the displays
  2. various display elements :
  • weather statistics,
  • a simple forecast, and
  • a heat index calculator
Let’s say this is the Weather Station
These are the displays, which may or may not represent the actual implementation.

Thought process

  • Weather station should collect the data, and notify the changes to the various devices.
  • In design terminology, the weather station is the subject, and the display devices are the observers.
  • Weather station is the single owner of the data, devices are dependent on it for any updates.
  • The common analogy often used here is that of publisher-consumer models.
  • The station is responsible in sending the collected data, it should not be bothered about how the observing devices use it.
  • Suppose a better index (e.g. WBGT) comes up and heat index becomes irrelevant, we should be able to freely add the new device, and remove the heat index device.
  • We would not want to modify the code for the subject, when the new observer (WBGT device) comes in.
  • Changes to either the subject or the observer should have no impact on each other.
  • Similarly, both should be reusable and independent to each other.

UML Class Diagram

Class Diagram showing the use of observer pattern (Source: Head First Design Patterns)
  • StatisticsDisplay is now StatDisplay

The Code

Note as the measurements change, observers immediately reflect the changes
Current conditions: 80.0F degrees and 65.0% humidity.
Avg/Max/Min temperature = 80.0/80.0/80.0
Forecast : Improving weather on the way!
Heat index is 82.955345
Current conditions: 82.0F degrees and 70.0% humidity.
Avg/Max/Min temperature = 81.0/82.0/80.0
Forecast : Watch out for cooler, rainy weather!
Heat index is 86.901260
Current conditions: 78.0F degrees and 90.0% humidity.
Avg/Max/Min temperature = 80.0/82.0/78.0
Forecast : More of the same.

Language specifics

Note: Go channels which are idiomatic way of handling data in concurrent code (routines) can be applicable to this use-case. Perhaps, I’ll cover that in another article.

type displayerObserver interface {
Displayer
Observer
}
...
func (sd *StatDisplay) update(temp float32, humidity float32, pressure float32) {
...
var d Displayer = sd
d.display()
...
}
// check statDisplay.go file for more detail

Takeaways

The book defines,

  1. Program to an interface, not an implementation.
  2. Favour composition over inheritance.
  3. Strive for loosely coupled designs between objects that interact.

Coder, Amateur Musician, Blogger, Quora writer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store