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. So if there is a function being passed as an argument, set the default execute method to the new function being provided. So self execute assignment and here, this is where we're using the types module.
So types method type function self. This allows us to dynamically find a new method to a class. If you don't get any reference, we're not doing anything so by default if you don't get any argument it will be simply executing the default execute method when the execute method is invoked.
Now let's define the replacement method which is strategy one. You can see it right here. In the replacement method, all we are doing is really using this print method to display the current value of the name attribute as well as the name of the replacement method which is method one. And this is how you know that strategy one is used.
And then we'll be doing the same thing for the second strategy and that's why I'm just copying and pasting this one. But you still have to make some changes so it will still be the print statement however I'm going to be changing the method name because this is method two not method one. So this has to be method two. Now we're ready to create our default strategy object and also create other strategy objects.
Let's see if other strategies also work. Let me create our default strategy first which is strategy, so type strategy. We'll then execute it and see what happens. Next we'll be creating other strategies but this time instead of not passing anything as an argument we'll be passing the name of the function or method we want to be using.
So type strategy but now we are passing the name of the function in this case strategy one. And then we'll set the name of the strategy which is simply strategy one. Next we have to invoke the execute method on this one. So type execute, and we'll create another strategy here with a different function name. Now this time the function name is strategy two.
So just type strategy but this time the name of the function is strategy two and then we'll set the name to strategy two. And finally we'll be invoking the execute method again. So now let's run the script and see what happens. Go to Tools, click on Build. As you can see the program works beautifully and the first message being displayed says default strategies used.
Second message says strategy one is used to execute method one, and then the third message states, strategy two is used to execute method two. One thing you really have to pay attention to here is that this execute method is the one that's being replaced dynamically. The implementation of this strategy pattern is much more straight forward when using Python especially because of the built in features such as attach module provided by the language Python.
- 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.