Maths encyclopedia and lessons  
Search

Mathematics Encyclopedia and Lessons

 
     
 

Lessons

Popular
Subjects

algebra
arithmetic
calculus
equations
geometry
differential equations
trigonometry
number theory
probability theory
more
 

References

applied mathematics
mathematical games
mathematicians
more
 
 

Strategy pattern

In 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

  • Facade memento strategy
  • Explicit strategy pattern

External links

01-04-2007 01:18:14
The contents of this article are licensed from Wikipedia.org
under the GNU Free Documentation License. How to see transparent copy