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. Creation Design Patterns
  • OOP Design Patterns
  • Python-Specific Design Patterns
  • Creation Design Patterns
  • Structural Design Patterns
  • Behavioral Design Pattern
  • Travaux Pratiques
    • TP

On this page

  • Creational Design Patterns in Python
    • 1. Singleton Pattern
      • 1.1 Implementation using __new__
      • 1.2 Implementation using a Decorator
      • 1.3 Pros and Cons
    • 2. Factory Method Pattern
      • 2.1 Example: Matrix Computation with Different Backends
      • 2.2 Benefits of Factory Method
    • 3. Abstract Factory Pattern
      • 3.1 Example: Cross-platform UI Components
      • 3.2 Benefits of Abstract Factory
    • 4. Builder Pattern
      • 4.1 Example: Custom Computer Builder
      • 4.2 Benefits of Builder
    • Conclusion

Creation Design Patterns

Cours
Fondamentaux
Learn creational design pattern
Author

Remi Genet

Published

2024-10-21

Creational Design Patterns in Python


Creational design patterns provide various object creation mechanisms, which increase flexibility and reuse of existing code.

1. Singleton Pattern

The Singleton pattern ensures a class has only one instance and provides a global point of access to it.

1.1 Implementation using __new__

1.2 Implementation using a Decorator

1.3 Pros and Cons

  • Pros:
    • Ensures a single instance
    • Global access point
    • Lazy initialization
  • Cons:
    • Violates Single Responsibility Principle
    • Can make unit testing difficult
    • Can hide bad design

2. Factory Method Pattern

The Factory Method pattern provides an interface for creating objects in a superclass, but allows subclasses to alter the type of objects that will be created.

2.1 Example: Matrix Computation with Different Backends

2.2 Benefits of Factory Method

  • Provides flexibility in object creation
  • Promotes loose coupling
  • Adheres to the Open/Closed Principle
  • Simplifies adding new types of objects

3. Abstract Factory Pattern

The Abstract Factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes.

3.1 Example: Cross-platform UI Components

3.2 Benefits of Abstract Factory

  • Ensures compatibility between created objects
  • Isolates concrete classes
  • Simplifies exchanging product families
  • Promotes consistency among products

4. Builder Pattern

The Builder pattern separates the construction of a complex object from its representation, allowing the same construction process to create different representations.

4.1 Example: Custom Computer Builder

4.2 Benefits of Builder

  • Allows step-by-step creation of complex objects
  • Can use the same construction code for different representations
  • Isolates complex construction code from business logic

Conclusion

Creational design patterns solve problems related to object creation in software design. They provide flexibility, improve code reusability, and help manage complexity in object-oriented systems. Each pattern has its specific use cases, and understanding them allows developers to choose the right tool for the job when designing software systems.

Back to top
Python-Specific Design Patterns
Structural Design Patterns

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