🐍 Strategy Design Pattern in Python
Tags: python, programming
Published: 2021-08-11 15:00:56

### 📗 Learn strategy design patterns to write better code in Python.

### <br>

While you are developing software applications, you might run into some problems implementing the feature you need. Software design patterns are typical solutions for some of these commonly occurring problems while developing software applications using Object-oriented design. In this article, let's learn about one of the popular design patterns, **Strategy** pattern, and its implementation in Python.

## Introduction

Before diving into the strategy pattern, you should be familiar with some of the basics concepts of [Object-Oriented Programming](https://en.wikipedia.org/wiki/Object-oriented_programming) (OOP). The entire concept of design patterns revolves around *classes* and *objects*. The design patterns are more high-level solutions for commonly occurring problems. They are like the blueprint to solve a specific problem. They are not confined to a single programming language. You can use design patterns in any programming language that supports object-oriented programming; the process will be the same while the syntax changes.
![design patterns](https://images.ctfassets.net/23aumh6u8s0i/4tPzU9nvm5jUwrXAeeHH3e/1badc171178aa953f7df05f08960f228/01_design-types.jpg)
There are several types of design patterns, including **Creational**, **Structural**, and **Behavioral** patterns. Creational patterns are about different ways to create objects that increase the flexibility of our code. Structural patterns are about relations between the objects, making larger structures flexible using objects and classes. Behavioral patterns are about effective communications and interactions between objects.

## Strategy

**Strategy Pattern** is a design pattern that enables our application to select algorithms at runtime, making our application flexible. The original book on [design patterns](https://en.wikipedia.org/wiki/Design_Patterns) written by [GoF](http://wiki.c2.com/?GangOfFour) states that "Strategy pattern intends to define a family of algorithms, [encapsulates](shorturl.at/rHJR7) each one, and
make them interchangeable." More specifically, it lets you define a set of algorithms that are interchangeable according to some factors at runtime. Strategy Pattern falls under the category of behavioral design patterns as it enables an algorithm's behavior to be selected at runtime.

## Usage

While developing software applications, you may have a few alternatives to accomplish something in your code. Depending on your client choices, data sources, or other factors, you want to do something different without changing the code. You often tend to define algorithms using conditional statements for different situations in the main class of the code. But it is not an elegant way of writing better code. It makes the main class of your code quite long, and it becomes too hard to maintain the application.

In situations like these, the strategy pattern is an ideal solution. The strategy pattern suggests you define classes, called *strategies*, for your algorithms of different situations. The strategy is referenced inside the main class, called _context_, and the code works according to that situation. The context does not select an appropriate strategy for the case. Instead, the client passes the desired strategy to the context.

For example, if you have a chess application, you can select the difficulty level between easy, medium, or hard. The computer chooses an algorithm according to the level you choose. It is one of the best examples where the strategy pattern is used.

Strategy pattern follows the [Open/close principle](https://en.wikipedia.org/wiki/Open%E2%80%93closed_principle); a software application is open for extension but closed for modification. It means you can add any number of additional strategies without modifying the main class. It makes your code more flexible and easy to maintain.

[Read more...](https://auth0.com/blog/strategy-design-pattern-in-python/?utm_source=content_synd&utm_medium=sc&utm_campaign=python)