What is Strategy Pattern?
Strategy pattern is a commonly used behavioural pattern1. Strategy Pattern is mostly a way to change the behaviour of an algorithm at runtime.
The strategy pattern:
- defines a family of algorithms,
- encapsulates each algorithm, and
- makes the algorithms interchangeable within that family.
Think twice before implementing this pattern. You have to be sure your need is to change an algorithm frequently. You have to clearly anticipate the future, otherwise, this pattern will be more expensive than a basic implementation.
- + Greater flexibility, reuse
- + Can change algorithms dynamically
- – Strategy creation & communication overhead
- – Expensive to create
- – This pattern can be expensive to maintain. If the representation of a class often changes, you will have lots of refactoring.