课程: Go Design Patterns

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Observer pattern example

Observer pattern example - Go教程

课程: Go Design Patterns

Observer pattern example

- [Instructor] In this exercise, we're going to implement a very simple example of the observer pattern. So here in my editor, I'm going to open the code for the subject.go file. So this code creates a type called data subject which will be the object that gets observed for changes by the observers, which we will get to in a moment. So the data subject contains a list of observers called data listener and a string field and you can see that I have defined an interface called observable which contains three methods. There's register observer, unregistered observer and notify all. And then, if we scroll down a bit, there are some methods that need to be implemented and we will come back to these, right? So next, let's open the code in the observer.go file and this code implements the type for an observer. So there's an interface named observer that defined a method called onUpdate which takes a string argument. There's…

内容