LearningTech

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:
    def __init__(self):
        pass

    def execute(self):
        print('Executing Strategy')