Strategy pattern
From CSSEMediaWiki
(Difference between revisions)
(Started the page) |
|||
Line 1: | Line 1: | ||
The strategy pattern is useful when there is a need to separate the implementation of a process from the class that uses it. | The strategy pattern is useful when there is a need to separate the implementation of a process from the class that uses it. | ||
There are several reasons that this can be useful. | There are several reasons that this can be useful. | ||
− | + | [[Image:Strategy pattern.jpg|frame| The strategy pattern]] | |
* There are several different versions of the algorithm to implement. | * There are several different versions of the algorithm to implement. | ||
* The algorithms are used in more than one place. [[Once and only once]] | * The algorithms are used in more than one place. [[Once and only once]] | ||
* There is a need to create an encapsulation boundary around the algorithm to hide implementation details. | * There is a need to create an encapsulation boundary around the algorithm to hide implementation details. | ||
* The class using the strategy implements many different behaviours choosing which behaviour to use with ''if'' statements. Each choice can be represented as a strategy instead simplifying code and making the intent explicit. | * The class using the strategy implements many different behaviours choosing which behaviour to use with ''if'' statements. Each choice can be represented as a strategy instead simplifying code and making the intent explicit. | ||
− | |||
− |
Revision as of 04:46, 11 September 2008
The strategy pattern is useful when there is a need to separate the implementation of a process from the class that uses it. There are several reasons that this can be useful.
- There are several different versions of the algorithm to implement.
- The algorithms are used in more than one place. Once and only once
- There is a need to create an encapsulation boundary around the algorithm to hide implementation details.
- The class using the strategy implements many different behaviours choosing which behaviour to use with if statements. Each choice can be represented as a strategy instead simplifying code and making the intent explicit.