Join Jungwoo Ryoo for an in-depth discussion in this video Strategy example, part of Python: Design Patterns.
- So this is our Strategy class and note…that we are importing the types module here…as you can see.…This doesn't work without the types module.…The types module supports dynamic creation of new types.…In this case we dynamically create a new method type.…The very first thing we'll be doing…in this Strategy Pattern class is to define…the (mumbles) method.…As you can see right here, and all we are doing here…is simply initializing the attribute called name,…right here to Default Strategy…every time the Strategy class is instantiated.…
The Strategy class also comes with a default method…called execute, the one you see right here.…This default method prints the current value…of the name attribute.…As you can see right here,…now this part…is the most important part of this…Strategy class definition and this is where we say,…if a reference to a function is provided…as an argument here…as you can see, we're replacing the default execute method…with the given function.…
So how do we say that in Python?…Type if function.…
- 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.