From the course: Python: Design Patterns
Unlock this course with a free trial
Join today to access over 24,400 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 attached 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 parenthesis, 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 subject. Type, for observer in self._observers:…
Contents
-
-
-
-
-
-
(Locked)
Observer1m 5s
-
(Locked)
Observer example6m 22s
-
(Locked)
Solution: Observer2m 4s
-
(Locked)
Visitor56s
-
(Locked)
Visitor Example6m 42s
-
(Locked)
Iterator1m 15s
-
(Locked)
Iterator example4m 32s
-
(Locked)
Strategy40s
-
(Locked)
Strategy Example5m 44s
-
(Locked)
Solution: Strategy4m 16s
-
(Locked)
Chain of Responsibility40s
-
(Locked)
Chain of Responsibility example5m 57s
-
(Locked)
-
-