DEV Community


Posted on

🐍 Strategy Design Pattern in Python

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

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.


Before diving into the strategy pattern, you should be familiar with some of the basics concepts of 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
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 Pattern is a design pattern that enables our application to select algorithms at runtime, making our application flexible. The original book on design patterns written by GoF states that "Strategy pattern intends to define a family of algorithms, encapsulates 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.


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; 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.


Discussion (0)