Strategy (design pattern)
E679880
Strategy is a behavioral design pattern that defines a family of interchangeable algorithms, encapsulates each one, and lets clients switch between them at runtime without changing their code.
Observed surface forms (1)
| Surface form | Occurrences |
|---|---|
| Strategy pattern | 1 |
Statements (50)
| Predicate | Object |
|---|---|
| instanceOf |
behavioral design pattern
ⓘ
software design pattern ⓘ |
| advantage |
improves code maintainability
ⓘ
improves testability of algorithms in isolation ⓘ promotes composition over inheritance ⓘ reduces conditional complexity ⓘ supports adding new algorithms without modifying existing client code ⓘ |
| alsoKnownAs | Policy pattern NERFINISHED ⓘ |
| applicableWhen |
a class has many conditional statements selecting different behaviors
ⓘ
clients should be able to choose algorithms at runtime ⓘ many related classes differ only in their behavior ⓘ there are different variants of an algorithm needed ⓘ |
| belongsToCategory | Gang of Four design patterns NERFINISHED ⓘ |
| contrastedWith | hard-coded conditional logic for algorithm selection ⓘ |
| definedIn | Design Patterns: Elements of Reusable Object-Oriented Software NERFINISHED ⓘ |
| definesRole |
ConcreteStrategy implements a specific algorithm
NERFINISHED
ⓘ
Context delegates algorithmic work to the Strategy object ⓘ Context maintains a reference to a Strategy object ⓘ Strategy defines a common interface for all supported algorithms ⓘ |
| describedByAuthor |
Erich Gamma
NERFINISHED
ⓘ
John Vlissides NERFINISHED ⓘ Ralph Johnson NERFINISHED ⓘ Richard Helm NERFINISHED ⓘ |
| disadvantage |
increases number of classes
ⓘ
may introduce overhead of indirection ⓘ requires clients to understand different strategies ⓘ |
| exampleUseCase |
compression with different compression algorithms
ⓘ
payment processing with different payment methods ⓘ routing with different pathfinding algorithms ⓘ sorting with different comparison strategies ⓘ |
| hasParticipant |
ConcreteStrategy
ⓘ
Context ⓘ Strategy interface NERFINISHED ⓘ |
| primaryIntent |
define a family of algorithms, encapsulate each one, and make them interchangeable
ⓘ
let algorithms vary independently from clients that use them ⓘ |
| relatedTo |
Decorator (design pattern)
NERFINISHED
ⓘ
Factory Method (design pattern) NERFINISHED ⓘ State (design pattern) NERFINISHED ⓘ Template Method (design pattern) NERFINISHED ⓘ |
| supportsFeature |
algorithm interchangeability
ⓘ
elimination of conditional logic based on algorithm choice ⓘ encapsulation of algorithms ⓘ open/closed principle ⓘ runtime selection of algorithms ⓘ single responsibility principle ⓘ |
| typicalImplementationLanguage |
C#
ⓘ
C++ NERFINISHED ⓘ Java NERFINISHED ⓘ JavaScript NERFINISHED ⓘ Python NERFINISHED ⓘ |
Referenced by (2)
Full triples — surface form annotated when it differs from this entity's canonical label.