Author image

Strategy Design Pattern


Definition:

Define a family of algorithms, encapsulate each one and make them interchangeable.

It is also known as 'policy' pattern. Instead of implementing a single algorithm directly, code receives run-time instructions as to which in a family of related algorithms to use.

Strategy is a flexible pattern, as it makes combining, adding, changing behaviors of an object easy as pie.

Design

  • create your algorithms as classes, or abstract classes
  • refine your algorithms by inheriting from them
  • create your target object Duck here
  • the target object has pointers to all the possible base algorithms it can support
  • create your object by supplying to it the behaviors you want to provide through its constructor
  • to assign new behaviors simply supply new...

1