Patrones de Dise帽o y Principios SOLID
Patrones de Dise帽o y Principios SOLID en Python
Principios SOLID
Principio de Responsabilidad 脷nica (SRP) en Python
Procesador de Pagos con Stripe en Python
Aplicar el Principio de Responsabilidad 脷nica (SRP)
Principio Abierto/Cerrado (OCP) en Python
Aplicar el Principio Abierto/Cerrado (OCP)
Principio de Sustituci贸n de Liskov (LSP) en Python
Aplicar el Principio de Sustituci贸n de Liskov (LSP)
Principio de Segregaci贸n de Interfaces (ISP) en Python
Aplicar el Principio de Segregaci贸n de Interfaces (ISP)
Principio de Inversi贸n de Dependencias (DIP) en Python
Aplicar el Principio de Inversi贸n de Dependencias (DIP)
Reestructuraci贸n del proyecto
Reestructuraci贸n de un proyecto en Python
Patrones de Dise帽o
Introducci贸n a los Patrones de Dise帽o
Patr贸n Strategy en Python
Implementando el Patr贸n Strategy
Patr贸n Factory en Python
Implementando el Patr贸n Factory
Patr贸n Decorator en Python
Implementando el Patr贸n Decorador: Mejora tu Servicio de Pagos
Patr贸n Builder en Python
Implementando el Patr贸n Builder: Construye Servicios de Pago
Patr贸n Observer en Python
Implementando el Patr贸n Observer
Patr贸n Chain of Responsibility en Python
Implementando el Patr贸n Chain of Responsibility: Flujo Eficiente de Validaciones
Patrones de Dise帽o y Principios SOLID en un Procesador de Pagos
You don't have access to this class
Keep learning! Join and start boosting your career
The Decorator Pattern is a structural design pattern that allows to dynamically add responsibilities to objects. This pattern is especially useful when we need to extend the functionality of an object without altering its original structure. Often, it is necessary to add additional logic without modifying the existing behavior, which is precisely the essence of what the Decorator Pattern offers.
This pattern allows for flexible composition of additional behaviors. If you have multiple decorators, you can combine them to augment the logic on a base behavior. Thus, it acts as a modular and flexible way for developers to adapt the functionality of their application to different situations without having to rewrite the code base.
The Decorator Pattern is useful when you need to combine multiple behaviors in a modular way. Imagine that you have a desired behavior and, around it, you need another behavior; in this case, the pattern allows you to compose them. Also, it is ideal for situations where you need to add functionality at runtime due to a specific condition in the code or context.
Implementing the Decorator Pattern may seem complex at first, but it can be simplified by following these five steps:
Define an interface or abstract class: This describes the behavior of the base object, which has the main behavior.
Implement the interface in concrete classes: Here, the behavior is abstracted to an interface and then implemented in several concrete classes.
Create an abstract decorator class: This class also implements the interface defined in the first step and acts as the basis for the decorators.
Implement concrete decorators: Concrete decorators extend the decorator class and contain the additional behavior that you want to implement.
Wrap basic objects with decorators: Finally, the basic objects are wrapped with the created decorators, allowing the composition of functionalities.
This process becomes more intuitive when applied in code, allowing to clearly visualize how each component interacts.
Suppose we have a system for sending notifications, and we want to add different forms of notification dynamically. Here is a basic implementation of the Decorator Pattern in Python:
# Interface for notificationclass Notificacion: def send(self): pass
# Concrete implementation of basic notificationclass NotificacionBasica(Notificacion): def send(self): return "Notification sent"
# Abstract decoratorclass class DecoratorNotificacion(Notificacion): def __init__(self, notificacion): self.notificacion = notificacion
def send(self): return self.notificacion.enviar()
# Decorator to add a message in the logclass NotificacionConLog(DecoradorNotificacion): def send(self): result = super().send() return f"{result} with log"
# Use decorator patternnotificacion = NotificacionBasica()notificacion_con_log = NotificacionConLog(notificacion)
print(notificacion_con_log.enviar()) # Output: "Notificacion enviada con registro en el log".
In this example, we have implemented a basic notification and subsequently added a decorator to include a log in the log, thus demonstrating how the Decorator Pattern can be used to modularize and extend functionalities.
Have you ever had the opportunity to apply the Decorator Pattern in a project? Let us know in the comments! This pattern is a valuable tool for many developers and continues to be relevant in modern programming. We encourage you to continue exploring this and other design patterns to enrich your skills as a developer.
Contributions 3
Questions 0
Want to see more contributions, questions and answers from the community?