-Now let's see how this whole thing works in practice by - -2
-using an example. We're going to consider a program that takes as - -3
-input a text file and produce it as output, a filtered - -4
-file. So basically it outputs a subset of the content of - -5
-this text file based on some filter. And we're going to have - -6
-four different types of filters. So the first one is - -7
-not filtering which means that the whole content of the text - -8
-file will be produced on the output. The second filter will output - -9
-only words that starts with t. So you'll take the text file - -10
-and simply ignore all of the words that do not start with - -11
-t. So in the output we'll have only those words that starts - -12
-with letter t. The third filter will produce in the output only - -13
-words that are longer than five characters. So all the other words - -14
-will be simply disregarded. And finally, the four filter will produce as - -15
-output only words in the text file that are palindromes, and in - -16
-case you don't know what a palindrome is, a palindrome is a word - -17
-that is the same whether you read it from left - -18
-to right or from right to left. For example, the - -19
-word kayak, you can read it in this direction, or - -20
-in this direction, and it's exactly the same word. So - -21
-let's see how this program could be implemented using a - -22
-strategy pattern. And let's do it for real as a - -23
-demo. What we're looking at here is the editor page - -24
-for Eclipse, open with the strategy pattern implementation for our example. - -25
-So what I'm going to do is that, I'm going to look at a - -26
-different part of implementation. And you will see that, you know, despite - -27
-the fact that it's slightly longer, it's really fairly simple, it's kind - -28
-of a straightforward implementation of what we just saw. As I just - -29
-said, what we are doing is basically building the strategy patterns that - -30
-allows for changing the strategies with which we're filtering an input file. - -31
-And we have different strategies, we'll look at those in detail, and - -32
-we said that the three participants for this pattern are the context, - -33
-the algorithm, which is the general interface and then the concrete - -34
-strategies, which are the concrete implementations of this algorithm. So let's - -35
-start by looking at the context. Which is this class here. - -36
-And as you can see it contains a reference at the current - -37
-strategy. We call this the check strategy, which is basically our - -38
-filter, and when the context is created by default it sets a - -39
-strategy to the old strategy. The old strategy is the one - -40
-that accepts all the input, so basically it doesn't filter out anything. - -41
-And we said that the context is the interface to the - -42
-outside world, right? So it has to provide the outside world - -43
-with a way of selecting the strategy, the specific algorithm to - -44
-be used, and it does that in this case by providing - -45
-this change strategy method. This method takes a strategy as input, - -46
-and simply replaces the current strategy with the one specified as - -47
-a parameter. And at this point, the context also will perform - -48
-the filtering. The filtering is pretty straightforward, so what it does is - -49
-that it opens a file that is passed as a parameter - -50
-so that this the file, the input file to be filtered. And - -51
-then it reads the file line by line and then splits - -52
-the lines into its composing words and then for each word in - -53
-each line, what it will do, it will basically invoke the - -54
-check method in the current strategy, which is basically the filtering method - -55
-and if the check method returns true, which basically means that - -56
-the word should be printed, it prints the word. Otherwise, it'll just - -57
-skip it. So basically the filter will return false for all the - -58
-words that have to be filtered out. Okay? This is the basic - -59
-way in which context works. Let's see how this is used in - -60
-our main method. The main method simply creates the context, reads the input file - -61
-from the arguments, and then what he does is simply as a - -62
-demonstration, it will perform the filtering using all the different filters. So - -63
-starting from the default one, which is the one that basically doesn't - -64
-do any filtering that reports all words, then it will switch to the - -65
-algorithm, that only considers the words that start with t, and - -66
-it will do that by invoking a change strategy and passing this - -67
-strategy as the argument, and then - -68
-performing the actual filtering through context. - -69
-And it will do exactly the same for the strategy that only - -70
-prints words that are longer than five and the one that - -71
-only prints words that are palindromes. So now let's look at the - -72
-actual algorithm. This is the interface, the algorithm interface. And you can - -73
-see that the only thing that the interface provides is this method, - -74
-which is the check method, that takes a string as input and will - -75
-return a boolean. So, basically, it's the boolean that we were seeing before. - -76
-The one that is true for the words that have to be printed - -77
-and false for the ones that have to be filtered out. Now, we have - -78
-all the different implementations of the algorithm, the simplest one is the all - -79
-algorithm, the simple return is always true, so all the words will be printed. - -80
-The second one starts with t, and again, without looking at the details - -81
-of implementations that don't really matter, what - -82
-it does is basically check that - -83
-the first character is t, and returns true in that case and - -84
-false otherwise. Similarly, for the LongerThan5 - -85
-algorithm, also in this case, this - -86
-will implement the check strategy interface, and the check will be performed - -87
-by checking that the word is longer than five characters and returning - -88
-true in that case and false otherwise. And finally the Palindrome check - -89
-is a little more complicated, but basically it just checks whether the - -90
-word is a Palindrome and returns true in that case. Okay, so - -91
-as I said, it doesn't really matter too much what is the specific - -92
-implementations of these matters. What matters is that we have - -93
-a general interface for the algorithm and then any different concrete - -94
-implementations of the algorithm that implement different strategies. So again, - -95
-this allows you to change the behavior of your class without - -96
-changing class. So we have this context class that does - -97
-different things when the filter method in invoked, depending on what - -98
-is the current strategy. So the behavior of the class can - -99
-change dynamically, and it changes dynamically every time that we change - -100
-the strategy. At this point, the way this whole thing works should - -101
-be clear, so what we're going to do is that we're going to go to - -102
-our console, and we're actually going to run the strategy pattern and see - -103
-what happens. So here I have a file, it's called foo.txt. And if - -104
-we look at the content of foo, you can see that it - -105
-says that this is just a test to assess how well this program - -106
-performs when used on files of text. And since it checks for - -107
-palindromes, we will also insert one such word, level. Level is a palindrome, - -108
-because you can read it from both sides. Okay, so let's - -109
-see what happens when we run our code. So we're going to - -110
-run java pattern.strategy.StrategyPattern which is - -111
-our class, and we going to fetch - -112
-foo.txt as an input, and let's go back to the beginning - -113
-of the output to see what happened exactly. You can see - -114
-here that for the default strategy, which was the old strategy, - -115
-the whole file is printed, so every word is printed. This - -116
-is just a test to assess and so on and so forth, - -117
-as expected. For the filter that only prints words that - -118
-start with t, only words that start with t are printed, - -119
-again, as expected. Similarly, for the filter that only prints words - -120
-that are longer than 5, and finally for the one that prints - -121
-palindromes. And here you can see that we actually have two - -122
-because the way in which this is implemented we'll also consider - -123
-single letter words as palindromes because you can read them from - -124
-both sides. But you definitely will also have level in the output. - -125
-And in case you want to play with this code yourself, I - -126
-have made this code and also the implementation for examples of other - -127
-design partners available as a compressed archive. And the archive is accessible - -128
-through a URL that is provided in the notes for the cost. -- cgit 1.4.1