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
Implementing the Observer pattern in a payment system significantly improves the system's ability to react to events such as successful and failed payments. This approach allows us to send automatic notifications to relevant systems when specific transactions occur. In this class, we will break down how to set up this pattern in Python, following the proper steps to promote code scalability and maintainability.
The first step in implementing the Observer pattern is to define the protocol that acts as an interface to the Listeners. In Python, we can create this protocol using the typing
module.
from typing import Protocol, TypeVar
T = TypeVar('T')
class Listener(Protocol[T]): def notify(self, event: T)-> None: pass
This simple protocol forces concrete Listeners to implement the notify
method, which will take an event of generic type T
, not returning any value.
The next step is to create a Listeners handler, which will allow Listeners to subscribe, unsubscribe and receive notifications.
from typing import Listfrom dataclasses import dataclass, field
@dataclassclass ListenersManager: listeners: List[Listener] = field(default_factory=list)
def subscribe(self, listener: Listener)-> None: self.listeners.append(listener)
def unsubscribe(self, listener: Listener)-> None: self.listeners.remove(listener)
def notify_all(self, event: T): for listener in self.listeners: listener.notify(event)
The ListenersManager
acts as the mediator, taking care of adding and removing listeners and notifying events to all subscribed listeners.
To demonstrate the practical use of the Observer pattern, we can create a specific Listener that notifies the accounting system about successful payments.
from listeners import Listener
class AccountabilityListener(Listener): def notify(self, event: T): print(f "Notifying the accounting system about the event: {event}")
This AccountabilityListener
simply prints a notification message upon receiving an event.
Finally, we must adapt our main class to incorporate the ListenersManager
and the specific Listeners we wish to use.
from listeners import ListenersManager, AccountabilityListener
class PaymentService: def __init__(self): self._listener_manager = ListenersManager() self._set_listeners()
def _set_listeners(self): accountability_listener = AccountabilityListener() self._listener_manager.subscribe(accountability_listener)
def process_transaction(self, payment_response): if payment_response.is_success: self._listener_manager.notify_all(f "Payment successful: {payment_response.transaction_id}") else: self._listener_manager.notify_all(f "Payment failed: {payment_response.transaction_id}")
In this example, the PaymentService
class is responsible for handling notification events. During a successful or failed transaction, the service uses ListenersManager
to notify all subscribed listeners.
Using the Observer pattern provides several benefits:
Integrating the Observer pattern into your payment system not only improves code organization and flow, but also increases efficiency in event handling. Keep exploring and learning more about design patterns to take your programming to the next level!
Contributions 2
Questions 2
Want to see more contributions, questions and answers from the community?