The Strategy Pattern

The strategy pattern Define a family of algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it

The strategy pattern

The strategy pattern enables you to use different rules or algorithms depending on the context they occur. For example in a system calculating the tax due on different assetts, different tax rules (algorithms) are used depending on the type of asset being considered.

The strategy pattern defines a family of algorithms. It helps to eliminate complex switches and conditioanls (if...elseif...else). The algorithms are invoked the same way (as they must all have the same interface). The class that uses the algorithm (Context) contains an abstract class (Strategy) that has an abstract method which specifies how to call the algorithm. Each derived class (ConcreteStrategy) implements the algorithm as needed. Typically, the responsibility for selecting which concrete implementation to use is done by the client object, and is given to the context object of the strategy pattern.

Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <c>, <cpp>, <drupal5>, <drupal6>, <java>, <javascript>, <php>, <python>, <ruby>. The supported tag styles are: <foo>, [foo].
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.