Programmation Orientée Objet en Python
  • Back to Main Website
  • Home
  • Introduction: Histoire et Concepts
    • Introduction: Histoire et Concepts
    • Histoire de la programmation
    • Première Structuration des données
    • Naissance de la POO
    • Python: tout n’est qu’objet
    • Python : Simplicité des objets et performance sous-jacente
    • Classes en Python : Concepts fondamentaux

    • Travaux Pratiques
    • Construire sa propre Liste
    • Construire son propre DataFrame
  • Encapsulation, Héritage, Composition et Dunder
    • Encapsulation, Heritage, Composition et Dunder
    • Encapsulation en Python
    • Héritage en Python : Concept et intérêt
    • Héritage vs Composition
    • Méthodes Dunder en Python
    • Python call Method: A Fun Exploration

    • Travaux Pratiques
    • TP: Heritage avec le pricing d’option
    • TP : Ajouter des méthodes dunder à DataFrameSimple
    • TP : Étendre la classe Liste avec des méthodes dunder
    • TP: Dunder Method with Tensor for Automatic Differentiation
  • Polymorphisme et Surcharge
    • Polymorphisme et Surcharge
    • Polymorphism in Object-Oriented Programming
    • Polymorphism in Python: Function Overloading and Type Checking
    • Class Creation: Standard vs type()
    • Type Hinting, Typing Module, and Linters in Python
    • Abstract Classes
    • Protocol Classes

    • Travaux Pratiques
    • TP
  • Decorators
    • Design Patterns
    • The decorator pattern
    • Decorator Practically
    • Built-in Decorators and Standard Library Decorators in Python
    • Practical Decorators in Python Libraries

    • Travaux Pratiques
    • TP: Monte Carlo Option Pricing with Decorators
    • TP: Optimizing Heston Model Monte Carlo Simulation
  • Project Management and Packaging
    • Project and Package
    • Organizing Python Projects
    • Understanding imports
    • Python Package Management and Virtual Environments
    • Unit Testing in Python

    • Travaux Pratiques
    • TP: Creating a Linear Regression Package
  • Design Patterns
    • OOP Design Patterns
    • Python-Specific Design Patterns
    • Creation Design Patterns
    • Structural Design Patterns
    • Behavioral Design Pattern

    • Travaux Pratiques
    • TP
  • Sujets de Projets possibles
    • Projets
    • Projets POO - 2024-2025
  • Code source
  1. Behavioral Design Pattern
  • OOP Design Patterns
  • Python-Specific Design Patterns
  • Creation Design Patterns
  • Structural Design Patterns
  • Behavioral Design Pattern
  • Travaux Pratiques
    • TP

On this page

  • Behavioral Design Patterns in Python
    • 1. Observer Pattern
      • 1.1 Example: Weather Station
      • 1.2 Benefits of Observer
    • 2. Strategy Pattern
      • 2.1 Example: Payment Processing
      • 2.2 Benefits of Strategy
    • 3. Command Pattern
      • 3.1 Example: Remote Control
      • 3.2 Benefits of Command
    • 4. State Pattern
      • 4.1 Example: Vending Machine
      • 4.2 Benefits of State
    • 5. Chain of Responsibility Pattern
      • 5.1 Example: Logger Levels
      • 5.2 Benefits of Chain of Responsibility
    • 6. Template Method Pattern
      • 6.1 Example: Beverage Preparation
      • 6.2 Benefits of Template Method
    • Conclusion

Behavioral Design Pattern

Cours
Fondamentaux
Découvrir les design paterns, et connaitre les plus standards
Author

Remi Genet

Published

2024-10-21

Behavioral Design Patterns in Python


Behavioral patterns are concerned with algorithms and the assignment of responsibilities between objects. They characterize complex control flow that’s difficult to follow at run-time.

1. Observer Pattern

The Observer pattern lets you define a subscription mechanism to notify multiple objects about any events that happen to the object they’re observing.

1.1 Example: Weather Station

1.2 Benefits of Observer

  • Supports the principle of loose coupling between objects
  • Allows sending data to many objects efficiently
  • Dynamic relationships can be established between objects at runtime

2. Strategy Pattern

The Strategy pattern lets you define a family of algorithms, put each of them into a separate class, and make their objects interchangeable.

2.1 Example: Payment Processing

2.2 Benefits of Strategy

  • Allows switching algorithms used inside an object at runtime
  • Isolates the implementation details of an algorithm from the code that uses it
  • Replaces inheritance with composition

3. Command Pattern

The Command pattern turns a request into a stand-alone object that contains all information about the request.

3.1 Example: Remote Control

3.2 Benefits of Command

  • Decouples classes that invoke operations from classes that perform these operations
  • Allows creating sequences of commands with a macro command
  • Supports undo operations

4. State Pattern

The State pattern lets an object alter its behavior when its internal state changes. It appears as if the object changed its class.

4.1 Example: Vending Machine

4.2 Benefits of State

  • Organizes code related to particular states into separate classes
  • Makes state transitions explicit
  • Simplifies the code by eliminating bulky state machine conditional statements

5. Chain of Responsibility Pattern

The Chain of Responsibility pattern lets you pass requests along a chain of handlers. Upon receiving a request, each handler decides either to process the request or to pass it to the next handler in the chain.

5.1 Example: Logger Levels

5.2 Benefits of Chain of Responsibility

  • Reduces coupling between components
  • Increases flexibility in assigning responsibilities to objects
  • Allows adding or removing responsibilities dynamically

6. Template Method Pattern

The Template Method pattern defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure.

6.1 Example: Beverage Preparation

6.2 Benefits of Template Method

  • Lets subclasses implement varying behavior
  • Avoids code duplication
  • Allows fine-grained control over the steps of an algorithm

Conclusion

Behavioral design patterns are crucial for managing algorithms, relationships, and responsibilities between objects. They improve communication between objects, make complex control flows more manageable, and help distribute responsibilities efficiently. By applying these patterns, developers can create more flexible and maintainable code structures that are easier to understand and modify.

Back to top
Structural Design Patterns
TP

Programmation Orienté Object en Python, Rémi Genet.
Licence
Code source disponible sur Github

 

Site construit avec et Quarto
Inspiration pour la mise en forme du site ici
Code source disponible sur GitHub