The observer pattern in Go
This is a continuation of the common design patterns I found in my old code series, which I started in a previous post.
The observer pattern is a design pattern in which an object (a subject) keeps track of all of its dependents (observers) and notifies them of any state changes.
In Go, the closest example of this pattern are the builtin channels and the use of goroutines:
|
|
Though, for multiple observers to be notified, you need to send the message once for each observer.
Another example is the rxjs lib and accompanying libs for other languages.
And to illustrate how an implementation of this pattern looks like, check the following example:
|
|
For more design pattern examples, please checkout rolandjitsu/go-design-patterns.
If you find this post helpful, please consider sponsoring.
Sponsor