Strategy Pattern

This lesson discusses how a set of policies, algorithms or strategies can be made interchangeable without affecting the clients using them.

What is it ?

The strategy pattern is one of the simpler patterns to comprehend. It allows grouping related algorithms under an abstraction, which the client codes against. The abstraction allows switching out one algorithm or policy for another without modifying the client.

The strategy pattern is formally defined as encapsulating algorithms belonging to the same family and making them interchangeable. The consumers of the common interface that the algorithms implement allow switching out one algorithm for another seamlessly.

Class Diagram

The class diagram consists of the following entities

  • Strategy
  • Concrete Strategy
  • Context
widget

Examples

Concrete algorithms implement the same interface. The context has the data the algorithm will act on. Together the context and the strategy interact to implement the chosen algorithm. Usually, clients ...