|
| 1 | + |
| 2 | +""" |
| 3 | +Principio OCP (Abierto-Cerrado) |
| 4 | +Establece que una clase debe estar abierta para su extension pero cerrada a la hora de modificarla |
| 5 | +""" |
| 6 | + |
| 7 | +# Ejemplo incorrecto |
| 8 | + |
| 9 | +#lectura de sensores |
| 10 | + |
| 11 | +class ReadSensors(): |
| 12 | + def read_sensors(self, sensor_type): |
| 13 | + if sensor_type == 'temperature': |
| 14 | + return self.read_sensors_temperature() |
| 15 | + elif sensor_type == 'humidity': |
| 16 | + return self.read_sensors_humidity() |
| 17 | + # en caso de querer agregar otro sensor se debe modificar esta clase |
| 18 | + |
| 19 | + def read_sensors_temperature(self): |
| 20 | + return 'Temperature 25 C ' |
| 21 | + |
| 22 | + def read_sensors_humidity(self): |
| 23 | + return 'Humidity 50% ' |
| 24 | + |
| 25 | +Read = ReadSensors() |
| 26 | + |
| 27 | +print(Read.read_sensors('temperature')) |
| 28 | +print(Read.read_sensors('humidity')) |
| 29 | + |
| 30 | +# Ejemplo correcto |
| 31 | + |
| 32 | +# clase comun para todos los sensores |
| 33 | +class Sensor(): |
| 34 | + def read(self): |
| 35 | + pass |
| 36 | + |
| 37 | +class ReadTemperature(Sensor): |
| 38 | + def read(self): |
| 39 | + return 'Temperature 34 C' |
| 40 | + |
| 41 | +class ReadHumidity(Sensor): |
| 42 | + def read(self): |
| 43 | + return 'Humidity 70%' |
| 44 | + |
| 45 | +class ReadSensors(): |
| 46 | + def __init__(self, sensor): |
| 47 | + self.sensor = sensor |
| 48 | + |
| 49 | + def read_sensor(self): |
| 50 | + return self.sensor.read() |
| 51 | + |
| 52 | +temperature = ReadSensors(ReadTemperature()) |
| 53 | +print(temperature.read_sensor()) |
| 54 | + |
| 55 | +humidity = ReadSensors(ReadHumidity()) |
| 56 | +print(humidity.read_sensor()) |
| 57 | + |
| 58 | +"""Dificultad Extra """ |
| 59 | +""" |
| 60 | + * DIFICULTAD EXTRA (opcional): |
| 61 | + * Desarrolla una calculadora que necesita realizar diversas operaciones matemáticas. |
| 62 | + * Requisitos: |
| 63 | + * - Debes diseñar un sistema que permita agregar nuevas operaciones utilizando el OCP. |
| 64 | + * Instrucciones: |
| 65 | + * 1. Implementa las operaciones de suma, resta, multiplicación y división. |
| 66 | + * 2. Comprueba que el sistema funciona. |
| 67 | + * 3. Agrega una quinta operación para calcular potencias. |
| 68 | + * 4. Comprueba que se cumple el OCP. |
| 69 | +""" |
| 70 | + |
| 71 | +# Clase comun para totas las operaciones |
| 72 | +class Operation(): |
| 73 | + def execute(self,a,b): |
| 74 | + pass |
| 75 | +# Clases con las operaciones |
| 76 | +class Addition(Operation): |
| 77 | + def execute(self, a, b): |
| 78 | + return a + b |
| 79 | + |
| 80 | +class Subtraction(Operation): |
| 81 | + def execute(self, a, b): |
| 82 | + return a - b |
| 83 | + |
| 84 | +class Multiplication(Operation): |
| 85 | + def execute(self, a, b): |
| 86 | + return a*b |
| 87 | + |
| 88 | +class Division(Operation): |
| 89 | + def execute(self, a, b): |
| 90 | + if b == 0 : |
| 91 | + raise ValueError("Cannot divide by zero") |
| 92 | + return a / b |
| 93 | + |
| 94 | +# clases de la interfaz |
| 95 | + |
| 96 | +class Calculator(): |
| 97 | + def __init__(self, operation): |
| 98 | + self.operation = operation |
| 99 | + |
| 100 | + def calculate(self , a , b): |
| 101 | + return self.operation.execute(a,b) |
| 102 | + |
| 103 | +addition = Calculator(Addition()) |
| 104 | +print(addition.calculate(10, 5)) |
| 105 | + |
| 106 | +subtraction = Calculator(Subtraction()) |
| 107 | +print(subtraction.calculate(10, 5)) |
| 108 | + |
| 109 | +multiplication = Calculator(Multiplication()) |
| 110 | +print(multiplication.calculate(10, 5)) |
| 111 | + |
| 112 | +division = Calculator(Division()) |
| 113 | +print(division.calculate(10, 5)) |
| 114 | + |
| 115 | +#agregando la operacion potencia |
| 116 | +class power(Operation): |
| 117 | + def execute(self, a, b): |
| 118 | + return a**b |
| 119 | + |
| 120 | +power = Calculator(power()) |
| 121 | +print(power.calculate(2, 2)) |
| 122 | + |
0 commit comments