课程: Python: Design Patterns (2021)

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Strategy example

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…

内容