|
| 1 | +from abc import ABC, abstractmethod |
| 2 | + |
| 3 | + |
| 4 | +# Incorrecto |
| 5 | +# Vehiculo esta dependiendo de otro modulo y no de una interfaz |
| 6 | +class Surtidor: |
| 7 | + def reponer_gasoleo(self): |
| 8 | + pass |
| 9 | + |
| 10 | + def reponer_gasolina(self): |
| 11 | + pass |
| 12 | + |
| 13 | + def reponer_queroseno(self): |
| 14 | + pass |
| 15 | + |
| 16 | + |
| 17 | +class Vehiculo1: |
| 18 | + def __init__(self): |
| 19 | + self.combustible = Surtidor |
| 20 | + |
| 21 | + def mostrar_repostaje(self): |
| 22 | + repostaje = self.combustible() |
| 23 | + print(f"Respostando {repostaje}") |
| 24 | + |
| 25 | + |
| 26 | +vehiculo1 = Vehiculo1() |
| 27 | +vehiculo1.mostrar_repostaje() |
| 28 | + |
| 29 | + |
| 30 | +# Correcto |
| 31 | +class Combustible(ABC): |
| 32 | + @abstractmethod |
| 33 | + def reponer(self) -> str: |
| 34 | + pass |
| 35 | + |
| 36 | + |
| 37 | +class SurtidorGasoleo(Combustible): |
| 38 | + def reponer(self) -> str: |
| 39 | + return "Gasoleo" |
| 40 | + |
| 41 | + |
| 42 | +class SurtidorGasolina(Combustible): |
| 43 | + def reponer(self) -> str: |
| 44 | + return "Gasolina" |
| 45 | + |
| 46 | + |
| 47 | +class SurtidorQueroseno(Combustible): |
| 48 | + def reponer(self) -> str: |
| 49 | + return "Queroseno" |
| 50 | + |
| 51 | + |
| 52 | +class Vehiculo: |
| 53 | + """Clase de alto nivel - recibe cualquier combustible""" |
| 54 | + |
| 55 | + def __init__(self, combustible: Combustible): |
| 56 | + self.combustible = combustible |
| 57 | + |
| 58 | + def mostrar_repostaje(self): |
| 59 | + repostaje = self.combustible.reponer() |
| 60 | + print(f"Respostando {repostaje}") |
| 61 | + |
| 62 | + |
| 63 | +vehiculo1 = Vehiculo(SurtidorGasoleo()) |
| 64 | +vehiculo1.mostrar_repostaje() |
| 65 | + |
| 66 | +vehiculo2 = Vehiculo(SurtidorGasolina()) |
| 67 | +vehiculo2.mostrar_repostaje() |
| 68 | + |
| 69 | +vehiculo3 = Vehiculo(SurtidorQueroseno()) |
| 70 | +vehiculo3.mostrar_repostaje() |
| 71 | + |
| 72 | + |
| 73 | +""" |
| 74 | +extra |
| 75 | +""" |
| 76 | + |
| 77 | + |
| 78 | +# Interfaz |
| 79 | +class InterfazNotificaciones(ABC): |
| 80 | + """Interfaz general de la que dependeran las clases de alto y bajo nivel""" |
| 81 | + |
| 82 | + @abstractmethod |
| 83 | + def notificar(self) -> str: |
| 84 | + pass |
| 85 | + |
| 86 | + |
| 87 | +# Modulos de bajo nivel |
| 88 | +class Email(InterfazNotificaciones): |
| 89 | + """Solo se usa para mandar emails""" |
| 90 | + |
| 91 | + def notificar(self) -> str: |
| 92 | + return "Enviando Email" |
| 93 | + |
| 94 | + |
| 95 | +class Push(InterfazNotificaciones): |
| 96 | + """Solo se usa para mandar notificaciones push""" |
| 97 | + |
| 98 | + def notificar(self) -> str: |
| 99 | + return "Enviando push" |
| 100 | + |
| 101 | + |
| 102 | +class Sms(InterfazNotificaciones): |
| 103 | + """Solo se usa para mandar Sms""" |
| 104 | + |
| 105 | + def notificar(self) -> str: |
| 106 | + return "Enviando Sms" |
| 107 | + |
| 108 | + |
| 109 | +# Modulo alto nivel |
| 110 | +class Notificador: |
| 111 | + """Se encarga de mandar notificaciones, no de saber su tipo""" |
| 112 | + |
| 113 | + def __init__(self, notificacion: InterfazNotificaciones): |
| 114 | + self.notificacion = notificacion |
| 115 | + |
| 116 | + def ejecutar_notificacion(self) -> None: |
| 117 | + notificacion = self.notificacion.notificar() |
| 118 | + print(notificacion) |
| 119 | + |
| 120 | + |
| 121 | +print("=== Sistema de Notificaciones ===\n") |
| 122 | + |
| 123 | +notificador_email = Notificador(Email()) |
| 124 | +notificador_email.ejecutar_notificacion() |
| 125 | +notificador_push = Notificador(Push()) |
| 126 | +notificador_push.ejecutar_notificacion() |
| 127 | +notificador_sms = Notificador(Sms()) |
| 128 | +notificador_sms.ejecutar_notificacion() |
0 commit comments