Strategy pattern
E679865
The Strategy pattern is a behavioral design pattern that enables selecting an algorithm’s implementation at runtime by encapsulating each algorithm in a separate interchangeable class.
Statements (47)
| Predicate | Object |
|---|---|
| instanceOf |
behavioral design pattern
ⓘ
software design pattern ⓘ |
| alternativeName | Policy pattern NERFINISHED ⓘ |
| applicableWhen |
a class defines many behaviors using conditional statements
ⓘ
algorithms contain data that clients should not know about ⓘ behavior should be selected or changed at runtime ⓘ different variants of an algorithm are needed ⓘ many related classes differ only in their behavior ⓘ |
| belongsTo | Gang of Four design patterns NERFINISHED ⓘ |
| category | behavioral pattern in object-oriented design ⓘ |
| consequence |
allows reuse of algorithms across different contexts
ⓘ
eliminates large conditional statements ⓘ enables dynamic switching of algorithms ⓘ increases number of objects and classes ⓘ makes algorithms easier to test in isolation ⓘ |
| defines | family of interchangeable behaviors ⓘ |
| definesRole |
ConcreteStrategy implements the Strategy interface
ⓘ
Context selects and uses a Strategy ⓘ Strategy declares an interface common to all supported algorithms ⓘ |
| describedIn | Design Patterns: Elements of Reusable Object-Oriented Software NERFINISHED ⓘ |
| documentedBy |
Erich Gamma
NERFINISHED
ⓘ
John Vlissides NERFINISHED ⓘ Ralph Johnson NERFINISHED ⓘ Richard Helm NERFINISHED ⓘ |
| enables | plug-and-play replacement of algorithms ⓘ |
| encapsulates | algorithm ⓘ |
| firstPublishedYear | 1994 ⓘ |
| hasIntent |
define a family of algorithms, encapsulate each one, and make them interchangeable
ⓘ
let the algorithm vary independently from clients that use it ⓘ |
| involves |
ConcreteStrategy
ⓘ
Context ⓘ Strategy interface ⓘ |
| promotes |
composition over inheritance
ⓘ
open-closed principle ⓘ single responsibility principle ⓘ |
| reduces | conditional logic for algorithm selection ⓘ |
| relatedTo |
Decorator pattern
NERFINISHED
ⓘ
State pattern NERFINISHED ⓘ Template Method pattern NERFINISHED ⓘ |
| separates | algorithm implementation from its usage ⓘ |
| supports |
runtime selection of algorithm
ⓘ
unit testing of algorithms independently of context ⓘ |
| typicalImplementationLanguage | object-oriented programming languages ⓘ |
| usedFor |
compression algorithm selection
ⓘ
payment method selection in e-commerce ⓘ routing or pathfinding strategy selection ⓘ sorting algorithm selection ⓘ |
Referenced by (3)
Full triples — surface form annotated when it differs from this entity's canonical label.