From the course: Python: Design Patterns
Unlock this course with a free trial
Join today to access over 24,600 courses taught by industry experts.
Solution: Observer - Python Tutorial
From the course: Python: Design Patterns
Solution: Observer
- [Instructor] How was the coding for the code challenge? Not too bad, right? We already created our subject, which is the code to monitor here. The viewer is our observer and is also created here. To attach the viewer to the core, type c.attach, parentheses, v. Let's set the temperature to 100, which will trigger the viewer to generate a string that says viewer, colon, space, core colon, space, 100, and vertical line. So type c.temp, assignment, 100. Now, let's detach the viewer by typing c.detach, parentheses, and then the viewer, v. Here, we'll set the core temperature to 90, c.temp, assignment, 90, which will not affect the viewer because it's now detached, therefore, no string will be generated in this case, therefore, the overall result is the notification that was generated earlier when the temperature was set to 100, which is viewer, colon, space, core, colon, space, 100, and a vertical line. Click the test my code button. We did it again.
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)
-
-