From the course: Python: Design Patterns (2021)
Unlock the full course today
Join today to access over 24,600 courses taught by industry experts.
Strategy example - Python Tutorial
From the course: Python: Design Patterns (2021)
Strategy example
- 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 they'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 function", which means if there is a function being…
Contents
-
-
-
-
-
-
(Locked)
Observer1m 5s
-
(Locked)
Observer example6m 22s
-
(Locked)
Visitor56s
-
(Locked)
Visitor example6m 42s
-
(Locked)
Iterator1m 15s
-
(Locked)
Iterator example4m 32s
-
(Locked)
Strategy40s
-
(Locked)
Strategy example5m 44s
-
(Locked)
Chain of responsibility1m 4s
-
(Locked)
Chain of responsibility example5m 57s
-
(Locked)
-
-