|
| 1 | +############################################################################### |
| 2 | +### EJERCICIO: ISP |
| 3 | +############################################################################### |
| 4 | + |
| 5 | +### SIN APLICAR ISP ### |
| 6 | +class Calculator: |
| 7 | + """ |
| 8 | + Una calculadora que suma, resta, multiplica y divide. |
| 9 | + """ |
| 10 | + def sum(self, a, b): |
| 11 | + pass |
| 12 | + |
| 13 | + def subtract(self, a, b): |
| 14 | + pass |
| 15 | + |
| 16 | + def multiply(self, a, b): |
| 17 | + pass |
| 18 | + |
| 19 | + def divide(self, a, b): |
| 20 | + pass |
| 21 | + |
| 22 | +# Si queremos crear una calculadora que solo sume y reste no podemos, |
| 23 | +# estamos obligados a implementar todos los métodos en nuestro objeto. |
| 24 | +my_simple_calculator = Calculator() |
| 25 | + |
| 26 | + |
| 27 | +### APLICANDO ISP ### |
| 28 | +# Separamos los métodos en diferentes clases, para poder crear nuestra calculadora simple. |
| 29 | +class Sum: |
| 30 | + def sum(self, a, b): |
| 31 | + return a + b |
| 32 | + |
| 33 | + |
| 34 | +class Subtract: |
| 35 | + def subtract(self, a, b): |
| 36 | + return a - b |
| 37 | + |
| 38 | + |
| 39 | +class Multiply: |
| 40 | + def multiply(self, a, b): |
| 41 | + return a * b |
| 42 | + |
| 43 | + |
| 44 | +class Divide: |
| 45 | + def divide(self, a, b): |
| 46 | + return a / b |
| 47 | + |
| 48 | + |
| 49 | +# Ahora sí podemos crear una calculadora que solo sume y reste |
| 50 | +class SimpleCalculator(Sum, Subtract): |
| 51 | + pass |
| 52 | + |
| 53 | + |
| 54 | +# Ejemplo de uso |
| 55 | +my_simple_calculator = SimpleCalculator() |
| 56 | +print(my_simple_calculator.sum(2, 2)) |
| 57 | +print(my_simple_calculator.subtract(2, 2)) |
| 58 | + |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +############################################################################### |
| 63 | +### DIFICULTAD EXTRA |
| 64 | +############################################################################### |
| 65 | + |
| 66 | +# Aplicamos una clase para cada funcion que puede realizar nuestra impresora |
| 67 | +class Printer: |
| 68 | + def print(self, file): |
| 69 | + return f"Printing {file}..." |
| 70 | + |
| 71 | + |
| 72 | +class Scanner: |
| 73 | + def scan(self, file): |
| 74 | + return f"Scaning {file}..." |
| 75 | + |
| 76 | + |
| 77 | +class Fax: |
| 78 | + def send_fax(self, file, address): |
| 79 | + return f"Sendinf {file} to {address}" |
| 80 | + |
| 81 | + |
| 82 | +# Creamos nuestras impresoras personalizadas |
| 83 | +class BlanckWhitePrinter(Printer): |
| 84 | + def print(self, file): |
| 85 | + return f"Printing {file} in black and white" |
| 86 | + def __str__(self) -> str: |
| 87 | + return "A black and White printer" |
| 88 | + |
| 89 | + |
| 90 | +class ColorPrinter(Printer): |
| 91 | + def print(self, file): |
| 92 | + return f"Printinf {file} in color" |
| 93 | + |
| 94 | + def __str__(self) -> str: |
| 95 | + return "A color printer" |
| 96 | + |
| 97 | + |
| 98 | +class MultifunctionDevice(Printer, Scanner, Fax): |
| 99 | + def __str__(self) -> str: |
| 100 | + return "A Multifunction device" |
| 101 | + |
| 102 | + |
| 103 | + |
| 104 | +# Creamos los objetos y empleamos los metodos. |
| 105 | +my_printers = [BlanckWhitePrinter(), ColorPrinter(), MultifunctionDevice()] |
| 106 | + |
| 107 | +for printer in my_printers: |
| 108 | + print() # Un espacio en blanco para mejorar la visualizacion |
| 109 | + print(printer) |
| 110 | + print(printer.print("documento.txt")) |
| 111 | + |
| 112 | + if isinstance(printer, Fax): |
| 113 | + print( printer. send_fax( "documento.txt", "[email protected]")) |
| 114 | + else: |
| 115 | + print("This print doesn't send fax") |
| 116 | + |
| 117 | + if isinstance(printer, Scanner): |
| 118 | + print(printer.scan("documento.txt")) |
| 119 | + else: |
| 120 | + print("This printer doesn't scan") |
| 121 | + print() |
| 122 | + |
| 123 | + |
0 commit comments