Design Patterns that will be covered in this article:
This design pattern defines a family of interchangeable objects that can be set or switched at runtime. Also, Strategy is categorized under Behavioral patterns because the Strategy pattern is ultimately about 1 object using another to do something.
This pattern has 3 parts in-total:
- The object using a strategy (typically, a view controller in an iOS app)
- The Strategy Protocol
- This protocol defines methods that every strategy must implement
- The strategies are objects that conform to the Strategy Protocol
When Should You Use It?
- Use the strategy pattern when you have 2 or more different interchangeable behaviors
- Unlike Delegation, the strategy pattern defines a family of objects
- Strategies are intended to be easily changeable at runtime whereas Delegation are often fixed at runtime
What Should You be Careful About?
- Be careful about pre-optimizing and overusing this pattern.
- If a behavior won't ever change, it's perfectly okay to code it in a controller / object.