From the course: Python: Design Patterns
Unlock the full course today
Join today to access over 24,000 courses taught by industry experts.
Observer example - Python Tutorial
From the course: Python: Design Patterns
Observer example
- [Instructor] We start with the subject class that represents what is being observed. The subject class keeps this list called _observers where references to all the observers are being kept. Note that this is a one to many relationship. There'll be only one subject to be monitored by multiple observers. Next is defining the attach method. This method accepts an observer as an argument. With this observer object, we'll first check if the observer is not already on the observer list. Type if observer not in self._observers: We'll try to append the observer to the list only when it's not already on the list. And that's our next statement: type self._observers append(observer). The next method is the detach method that removes an observer from the observer list. The last method is our notify method. This is the most important method in the subject class because it notifies all the observers when there is a change in the…
Contents
-
-
-
-
-
-
(Locked)
Observer1m 5s
-
(Locked)
Observer example6m 22s
-
(Locked)
Visitor56s
-
(Locked)
Visitor example6m 42s
-
(Locked)
Iterator1m 15s
-
(Locked)
Iterator example4m 32s
-
(Locked)
Strategy40s
-
(Locked)
Strategy example5m 44s
-
(Locked)
Chain of responsibility1m 4s
-
(Locked)
Chain of responsibility example5m 57s
-
(Locked)
-
-