Join Jungwoo Ryoo for an in-depth discussion in this video Observer example, part of Python: Design Patterns.
- So we start with the Subject class…which represents what is being observed.…So the Subject class…keeps this list called _observes…which is where references to all the observers…are being kept.…So note that this is…a one to many relationship.…There'll be only one subject…who will be observed by…multiple observers.…Next is defining the attach method…and this method accepts an observer…as an argument.…
So with this observer object,…we'll be, first of all, checking…if the observer is not already…in the observers list.…So, add if observer…not in…self_observers…So only when this observer is not already…in the observers list…then we'll try to append the observer to the list.…So that's the next statement.…
So self_observers.append…The next method is the detach method…and this is simply removing the observer…from the observers list.…And then the last method is our notify method.…This is probably the most important method…in the subject class…because this is what notifies all the observers…when there is a change…in the subject.…
So what we want to say is…
- Understanding design patterns
- Best design practices: consistency, completeness, and correctness
- Working with creational patterns
- Working with structural patterns
- Working with behavioral patterns
Skill Level Intermediate
Q: In the strategy pattern example, why does the code keep executing the default function rather than the alternate?
A: The programming demonstration skips the step of defining what the strategy pattern should be when an alternate function name is provided
as an argument as shown below.
1. Understanding Design Patterns
2. Creational Patterns
3. Structural Patterns
4. Behavioral Patterns
5. Design Best Practices
- Mark as unwatched
- Mark all as unwatched
Are you sure you want to mark all the videos in this course as unwatched?
This will not affect your course history, your reports, or your certificates of completion for this course.Cancel
Take notes with your new membership!
Type in the entry box, then click Enter to save your note.
1:30Press on any video thumbnail to jump immediately to the timecode shown.
Notes are saved with you account but can also be exported as plain text, MS Word, PDF, Google Doc, or Evernote.