FREELessons: 12Length: 1.4 hours

• Overview
• Transcript

# 4.4 Strategy

Do you find yourself needing to perform different processes on a single entity? The strategy pattern is what you need to implement. I'll show you how in this lesson.

## 5.Conclusion1 lesson, 01:06

### 4.4 Strategy

The strategy pattern is difficult to explain at least in my opinion. Because the definition talks about taking a class and extracting functionality and algorithms into another class. So that you can then modify the behavior and functionality of the original class with other classes. And that just kinda muddies in my mind. So I like to think of the strategy as this, it allows us to change the functionality or behavior of an object at run time. And of course it's going to be much clearer when we have some code. So we're going to start with a class called NameList. And it's going to be nothing more than a list of names. We're going to have a public field simply called names. And then, let's have our constructor that is going to accept an array of names. And so, that is what we are going to store within our names field. So, the idea behind a strategy is this, we have this NameList class, and we want to serialize this information. But, of course, there are different formats that we might want to serialize to. So, one option would be to write methods inside of this class, so that we could export to those different formats. So we could have a method for exporting to JSON, we could have one to XML, we can have another one for CSV, and so on, and so forth. And the more formats we support, the more methods we have and of course, the more bloated this class becomes. And you can make the argument that this NameList class doesn't really need all of this extra functionality. Because the purpose of a name list is to just store a list of names. So the strategy pattern allows us to extract all of this functionality into separate classes. So that then the NameList class is just responsible for the name list itself. And the other classes that we create are then responsible for serializing that name list. Now there are different ways that we can implement this. But one of them is to have a method inset of this class for the purpose of serialization. So we're going to call that toString. And then we are going to pass in an object to our toString method that will then perform the serialization. So let's go ahead and define an interface for that, and we'll just call this INameListSerializer. We could Call this INameList strategy because that's where the strategy aspect comes into play. But I'm gonna to call this serializer because that makes more sense to me. And our strategies are going to have a method called output and it's going to accept a NameList object and we'll just call that NameList. And so this output method is then going to go through the process of serializing the NameList into whatever we want. So for this toString method, we're going to pass in our serializer and we'll call this $serializer. And then we will simply return$serializer output and then we will pass in this. So that's the idea. So now we can implement a class that implements our i NameList serializer. And the first one is going to be for JSON. So we'll call this JsonNameListSerializer and we don't need a constructor or anything like that. All we need is our output method. So public function output. We are accepting the name list object and inside of this method, all we are going to do is return JSON in code. We'll use our name list objects and the name's property because that is what we want to serialize. So now we would use this code like this. We'll have our names = newNameList. And as far as the names are concerned let's have Jeremy, Jason, Jeffrey. Now let's have some female names as well. Jennifer, there are some with two ns, one f, others with one n, and two fs. And then let's have Samantha, something that's going to be completely different. So we have our names and now we want to serialize this. So let's just do this, we'll echo names to string and then we will pass in a new Jsonnamelistserializer. And then that is going to serialize our names into Json. So let's run this php strategy and we get a fatal error. The problem is that our Json serializer does not implement our interface. And that is my fault. I got ahead of myself. So we want to implement the I NameList serializer name space, I'm sorry, interface. And now we run it and we have our list of names in JSON format. And so now we're told that we need to support CSV as well. So let's just copy our JSON name list serializer class. Let's change the name to csvnameListSerializer and instead of using json_encode we're going to use the implode function. Our separator is going to be a comma and then we have our array. So now all we have to do is just change the object that we passed to the two stream method. We're using a different strategy now. So if we go back to the console and run it, we have our names in CSV format. Now, there is an alternative implementation, and it involves removing the two string method from the nameless class. Because, the idea is that the NameList class just needs the properties of methods for that class to operate. So it doesn't need any method for exporting that information. That's what we would use the strategy for. So instead of using the two string method what we would do is this. Let's create a variable called csvSerializer and then we will new up CsvNameListSerializer. That's difficult to type and say at the same time. That's why I said it that way. And then we would use that object to then serialize our names. So we get the same results. We're still using our strategy and the strategy pattern. But we're doing so slightly differently. Now you get to choose which approach you want to take, because both are correct, it just really depends upon your class. Doesn't make sense to have a method to use the strategies. If so then define the method inside of the class, if not then use the strategies externally. Either way you'll get the same results.

Back to the top