|
|
|
|
|
Strategy PatternIn computer programming, the strategy pattern is a particular software design pattern, whereby algorithms can be selected on-the-fly at runtime depending on conditions, like strategies in a war situation. The strategy pattern is useful for situations where it is necessary to dynamically swap the algorithms used in an application. The strategy pattern is intended to provide a means to define a family of algorithms, encapsulate each one as an object, and make them interchangeable. The strategy pattern lets the algorithms vary independently from clients that use them. The following class diagram shows the strategy pattern basically consists of a context class and a set of strategy classes. An abstract strategy class is defined that custom strategy classes can implement. Each strategy object implements the algorithm that makes it unique. The client instantiates the strategy class and passes it as an argument when it calls the constructor of the context class. The client is then able to call methods on the context object and any strategy specific methods as necessary. Because the context creates an instance of the abstract strategy, the client is effectively using polymorphism to call strategy-specific methods on the context object. See also External links
|
 |
| |
|
|