Behavioral

Strategy

Description

Lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.

Where to use

  • Sorting algorithms.
  • \n
  • Payment methods.
  • \n
  • Route planning.

Real World Example

A GPS App. You can switch routing strategy: 'Walking', 'Driving', or 'Public Transport'. The map logic remains the same.

Code Example

class Strategy:\n    def __init__(self):\n        pass\n\n    def execute(self):\n        print('Executing Strategy')