Strategy Pattern
Problem
You have more than one way to solve a problem but you need to choose (or even switch) between these methods at run time.
Solution
Encapsulate your algorithms inside of Strategy objects.
Given an unsorted list, for example, we can change the sorting algorithm under different circumstances.
###The base class:
###The strategies:
###Using the strategies:
Discussion
“No plan survives first contact with the enemy”, nor users, but we can use the knowledge gained from changing circumstances to adapt. Near the end of the example, for instance, the newest item in the array now lies out of order. Knowing that detail, we can then speed the sort up by switching to an algorithm optimized for that exact scenario with nothing but a simple reassignment.
Exercises
- Expand
StringSorter
into anAlwaysSortedArray
class that implements all of the functionality of a regular array but which automatically sorts new items based on the method of insertion (e.g.push
vs.shift
).