|
| 1 | +""" |
| 2 | + * EJERCICIO: |
| 3 | + * Explora el "Principio SOLID de Inversión de Dependencias (Dependency Inversion |
| 4 | + * Principle, DIP)" y crea un ejemplo simple donde se muestre su funcionamiento |
| 5 | + * de forma correcta e incorrecta. |
| 6 | +""" |
| 7 | +#INCORRECTO |
| 8 | +class OtherPaymentService(): |
| 9 | + def paypal_payment(self,quantity:int|float): |
| 10 | + print(f"Estas pagando {quantity} EUR a través de Paypal") |
| 11 | + |
| 12 | + def stripe_payment(self,quantity:int|float): |
| 13 | + print(f"Estas pagando {quantity} EUR a través de Stripe") |
| 14 | + |
| 15 | +other_payment_service = OtherPaymentService() |
| 16 | +other_payment_service.paypal_payment(255.30) |
| 17 | +other_payment_service.stripe_payment(125.50) |
| 18 | +print("\n") |
| 19 | + |
| 20 | +#CORRECTO |
| 21 | +from abc import ABC, abstractmethod |
| 22 | + |
| 23 | +class PaymentService(ABC): |
| 24 | + @abstractmethod |
| 25 | + def payment(self,quantity:int|float): |
| 26 | + pass |
| 27 | + |
| 28 | +class PayPalPaymentService(PaymentService): |
| 29 | + def payment(self,quantity:int|float): |
| 30 | + print(f"Estas pagando {quantity} EUR a través de Paypal") |
| 31 | + |
| 32 | +class StripePaymentService(PaymentService): |
| 33 | + def payment(self,quantity:int|float): |
| 34 | + print(f"Estas pagando {quantity} EUR a través de Stripe") |
| 35 | + |
| 36 | +class PaymentProcessor(): |
| 37 | + def process_payment(self,payment_service:PaymentService,quantity:int|float): |
| 38 | + payment_service.payment(quantity) |
| 39 | + |
| 40 | +paypal_payment = PayPalPaymentService() |
| 41 | +stripe_payment = StripePaymentService() |
| 42 | +payment_processor = PaymentProcessor() |
| 43 | +payment_processor.process_payment(payment_service=paypal_payment,quantity=255.30) |
| 44 | +payment_processor.process_payment(payment_service=stripe_payment,quantity=125.50) |
| 45 | +print("\n") |
| 46 | + |
| 47 | +""" |
| 48 | +* DIFICULTAD EXTRA (opcional): |
| 49 | + * Crea un sistema de notificaciones. |
| 50 | + * Requisitos: |
| 51 | + * 1. El sistema puede enviar Email, PUSH y SMS (implementaciones específicas). |
| 52 | + * 2. El sistema de notificaciones no puede depender de las implementaciones específicas. |
| 53 | + * Instrucciones: |
| 54 | + * 1. Crea la interfaz o clase abstracta. |
| 55 | + * 2. Desarrolla las implementaciones específicas. |
| 56 | + * 3. Crea el sistema de notificaciones usando el DIP. |
| 57 | + * 4. Desarrolla un código que compruebe que se cumple el principio. |
| 58 | +""" |
| 59 | +class MessagingChannel(ABC): |
| 60 | + @abstractmethod |
| 61 | + def send_message(self,message:str): |
| 62 | + pass |
| 63 | + |
| 64 | +class SMSChannel(MessagingChannel): |
| 65 | + def send_message(self,message:str): |
| 66 | + print(f"Estás enviando a través de SMS el siguiente mensaje:\n\"{message}\"\n") |
| 67 | + |
| 68 | +class PushChannel(MessagingChannel): |
| 69 | + def send_message(self,message:str): |
| 70 | + print(f"Estás enviando a través de PUSH el siguiente mensaje:\n\"{message}\"\n") |
| 71 | + |
| 72 | +class EmailChannel(MessagingChannel): |
| 73 | + def send_message(self,message:str): |
| 74 | + print(f"Estás enviando a través de PUSH el siguiente mensaje:\n\"{message}\"\n") |
| 75 | + |
| 76 | +class NotificationSystem(): |
| 77 | + def send_notification(self,message:str,channel:MessagingChannel): |
| 78 | + channel.send_message(message) |
| 79 | + |
| 80 | +sms_channel = SMSChannel() |
| 81 | +push_channel = PushChannel() |
| 82 | +email_channel = EmailChannel() |
| 83 | +notification_system = NotificationSystem() |
| 84 | + |
| 85 | +notification_system.send_notification(message="Alex, ya tienes tu pedido preparado",channel=sms_channel) |
| 86 | +notification_system.send_notification(message="Alex, tienes un nuevo mensaje en tu bandeja de entrada",channel=push_channel) |
| 87 | +notification_system.send_notification(message="Alex, aquí tienes la newsletter quincenal de la comunidad",channel=email_channel) |
0 commit comments