Strategy Pattern
Intent
Define a family of algorithms, encapsulated each one and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.
AKA
Policy
Motivation
Avoid problems such as hard-wiring algorithms into classes that require them.
Applicability
Use the Strategy pattern when
- many related classes differ only in their behavior.
- You need different variants of an algorithm
- An algorithm uses date that clients shouldn't know about.
- A class defines many behaviors and these appear as multiple-conditional statements, move related conditional brances into theiw own Strategy class.
Diagrams
Non-Software Example