For the Strategy pattern, Reynald walks through the code to explain the structure, classes and methods of how the sample application. After the walkthrough, you should be ready to add code that will be use to execute the program. Key participants from the class diagram discussed that will be mentioned include: Strategy, ConcreteStrategy and Context
- [Instructor] We're going to walk through the code to demonstrate the strategy pattern. So a few key participants that I want you to pay attention to are SortedList, which is our context, SortStrategy, which is our strategy, and then our concrete strategies, which include QuickSort, ShellSort, and MergeSort. So as we scroll down in our solution, the first thing that you'll see starting on line 26, is our SortStrategy abstraction.
And that defines a message stub that's called Sort, which takes in a list of string variables. Right below it is our concrete strategy, QuickSort. And this is where the algorithm would be done for this particular form of sorting. So our list is going to have content of whatever we decide to put in there, in our case, later on you'll see that they're just names. Next you'll see our first concrete strategy which is QuickSort. And here we'll pass in our content, which is our list of, and you're going to see this later on, their names, and by default the sorting mechanism is going to be a form of QuickSorting.
So the sorting will be done, and then we just write out to the console what was done, which is QuickSorted list. The next thing we have here is our ShellSorting class, that's our next concrete strategy. And here is where you would have all the algorithm for Shell Sorting. In this particular case, we didn't go ahead and implement it because once we sorted one way versus another, in the output it's going to look the same, so I left that not implemented but you get the idea.
This is where you would put in the logic to sort it in whatever way that you want. And in this particular case, it's for ShellSorting. And of course on line 51, we just write out that it's been ShellSorted. And likewise, starting on line 58, we have our MergeSort concrete strategy. And the same applies here. That logic would be within this method too. Starting on line 70, we have our sorted list class, and that is our context.
This is where we set up our list collection, set a private variable to keep track of our SortStrategy, and then a few methods, one to set the SortStrategy, which takes in the SortStrategy in the perimeter, and and then assigns that to our private variable, and then a method for adding a name to a list, one for sorting, another method for sorting, and within there you can see that we pass in our list to be sorted.
And then we iterate over the list and display the results. And that's about it. Why don't we go ahead now and write the client code and see how it comes out on the screen.
- Why use design patterns?
- Prototype pattern
- Bridge pattern
- Façade pattern
- Flyweight pattern
- Proxy pattern
- Memento pattern
- State pattern
- Strategy pattern
- Template Method pattern