Breaking down the Strategy pattern
As part of the behavioral family of design patterns, the Strategy pattern is all about decoupling calling classes from how work gets done. Instead, the calling code (or client) chooses how it wants something done and delegates the execution to a middleman and an algorithm object with all the required data. Again, this might sound like the Command or State patterns, but using strategies is a different approach to similar problems. The Strategy pattern is useful when:
- You have a hierarchy of related classes that only differ in how they get something done.
- You need different variations of the same algorithm (families of behavior) and they should be interchangeable at runtime.
- You need algorithms to be decoupled from the client classes that use them.
Think of any gaming console you’ve ever had that required a disk or cartridge. The console itself doesn’t need to understand the differences between every game...