Skip to main content

Command Palette

Search for a command to run...

Business Optimization with the Strategy Pattern in Python: Practical Application with Dynamic Discounting

Updated
3 min read
Business Optimization with the Strategy Pattern in Python: Practical Application with Dynamic Discounting

Definition

The Strategy pattern allows defining a family of algorithms, encapsulating each one, and making them interchangeable. This way, the behavior of an object can be modified dynamically without altering its internal structure. It is especially useful in business contexts where business rules must change in real-time, such as discount systems, commissions, or pricing.

Strategy Method – Python Design Patterns | GeeksforGeeks

Example in Python

Below is an implementation of the Strategy pattern to apply different types of discounts based on the type of client:

from abc import ABC, abstractmethod

# Base strategy
class DiscountStrategy(ABC):
    @abstractmethod
    def apply_discount(self, amount: float) -> float:
        pass

# Concrete strategies
class RegularDiscount(DiscountStrategy):
    def apply_discount(self, amount: float) -> float:
        return amount * 0.95

class VIPDiscount(DiscountStrategy):
    def apply_discount(self, amount: float) -> float:
        return amount * 0.85

class CorporateDiscount(DiscountStrategy):
    def apply_discount(self, amount: float) -> float:
        return amount * 0.75

# Context
class DiscountContext:
    def __init__(self, strategy: DiscountStrategy):
        self._strategy = strategy

    def set_strategy(self, strategy: DiscountStrategy):
        self._strategy = strategy

    def get_discounted_price(self, amount: float) -> float:
        return self._strategy.apply_discount(amount)

# Example usage
if __name__ == "__main__":
    amount = 1000
    context = DiscountContext(RegularDiscount())
    print(f"Regular: ${context.get_discounted_price(amount)}")

    context.set_strategy(VIPDiscount())
    print(f"VIP: ${context.get_discounted_price(amount)}")

    context.set_strategy(CorporateDiscount())
    print(f"Corporate: ${context.get_discounted_price(amount)}")

Explanation

  • A common interface DiscountStrategy is defined with the method apply_discount.

  • The classes RegularDiscount, VIPDiscount, and CorporateDiscount implement this interface, encapsulating different discount logics.

  • The class DiscountContext holds a reference to a strategy that can change dynamically using the set_strategy method.

This allows new rules to be applied without modifying existing context code, favoring reuse and maintainability.

Automation with GitHub Actions

To ensure the code works correctly after each change, a CI automation can be used with GitHub Actions.

Workflow file (.github/workflows/python.yml):

name: Python CI

on:
  push:
    branches: [ main ]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: '3.10'

      - name: Run script
        run: python strategy_discount.py

✅ This pipeline performs:

  • Repository checkout.

  • Sets up Python 3.10.

  • Executes the strategy_discount.py file.

🎥 Watch code explanation on YouTube: https://www.youtube.com/watch?v=lC-fBlfUM8k

The full source code and workflow can be explored here:

https://github.com/jf2021070309/strategy-pattern-business-rules-python

Pattern Comparison

PatternLanguageMain PurposeKey Benefit
RepositoryJavaAbstract data access to keep business logic decoupledFacilitates unit testing and switching data sources
StrategyPythonDynamically switch algorithms without modifying the contextChanges behavior at runtime
Unit of WorkRubyManage a set of operations as a single transactionEnsures consistency across multiple actions

Conclusion

The Strategy pattern is a powerful tool to manage variable behaviors such as discounts, fees, or commissions. When applied in Python, it provides greater flexibility in business systems without sacrificing clarity or scalability. Moreover, compared with other patterns like Repository in Java or Unit of Work in Ruby, each one fulfills specific roles in enterprise software design, contributing to clean, efficient, and adaptable solutions.

A

I found this article on how to apply the Strategy pattern in Python to implement dynamic discounts very useful. It clearly explains how to structure code to make it more flexible and easier to maintain. The practical examples help you understand how to adapt discount strategies to different situations, which is very useful for optimizing processes in real-world applications.

E

I really liked how this article explained the Strategy Pattern through a practical business scenario. The Python example with different client types made the concept easy to understand and highlighted how flexible and maintainable this pattern can be in real-world applications.

E

This example of the Strategy pattern applied to commercial discounts in Python masterfully illustrates how design patterns transcend the technical to become business modeling tools.

E

I found the article very clear in explaining how the Strategy pattern can improve the flexibility of business systems. I really liked how it was applied to dynamic discounting and how each logic was encapsulated without altering the core code.

B

This article provides a clear and practical explanation of the Strategy pattern, particularly in the context of dynamic discounting in business systems. The Python example is concise and demonstrates how flexible and powerful this design pattern can be when applied correctly. Integrating the Strategy pattern with automation via GitHub Actions shows an understanding of real-world development practices, where CI/CD pipelines are essential.