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.
Strategy Example - Python Tutorial
From the course: Python: Design Patterns
Strategy Example
- [Instructor] This is our strategy class. Note that we're importing the types module here. This code doesn't work without the types module. The types module supports the dynamic creation of new types. In this case, we dynamically create a new method type. The very first thing we'll do in this strategy pattern class is defining the init method. All we're doing here is initializing the attribute called name. We set it to Default Strategy every time the strategy class is initiated. The strategy class also comes with a default method called execute. This default method prints the current value of the name attribute. Now, here is the most important part of this strategy class definition. This is where we say, if a reference to a function is provided as an argument here, we're replacing the default execute method with a given function. So how do we say that in Python? Type if, space, function. Which means, if there is a function being passed as an argument, then set the default execute…
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)
-
-