Strategy Pattern

(from Design Patterns)

Synopsis

Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithms vary independently from clients that use it. A Decorator lets you change the skin of an object; a Strategy lets you change the guts.

Context

The algorithm that a object uses is highly variable and can be reused elsewhere.

Forces

Solution

Encapsulate the different algorithms by defining objects for them (strategy objects). Objects which use the algorithm hold a reference to the strategy object. Switch algorithms by switching strategy objects.

Consequences

Implementation

See Design Patterns for sample code.

Known Uses


Thomas Minka
Last modified: Sat Jan 11 17:56:02 EST 1997