|
| 1 | +""" |
| 2 | + Princpicio Abierto Cerrado (OCP) |
| 3 | + Explora el "Principio SOLID Abierto-Cerrado (Open-Close Principle, OCP)" |
| 4 | + y crea un ejemplo simple donde se muestre su funcionamiento |
| 5 | + de forma correcta e incorrecta. |
| 6 | +""" |
| 7 | +# Ejemplo calculo de salario de un empleado |
| 8 | +from abc import ABC, abstractmethod |
| 9 | +from typing import Any |
| 10 | + |
| 11 | + |
| 12 | +class Employee: |
| 13 | + def __init__(self, name, base_salary): |
| 14 | + self.name = name |
| 15 | + self.base_salary = base_salary |
| 16 | + |
| 17 | + def calculate_salary(self): |
| 18 | + return self.base_salary |
| 19 | + |
| 20 | + |
| 21 | +class Part_time_employee(Employee): |
| 22 | + def __init__(self, name, base_salary, hr_worked): |
| 23 | + super().__init__(name, base_salary) |
| 24 | + self.hr_worked = hr_worked |
| 25 | + |
| 26 | + def calculate_salary(self): |
| 27 | + return self.base_salary*self.hr_worked |
| 28 | + |
| 29 | + |
| 30 | +class Comissioned_employee(Employee): |
| 31 | + def __init__(self, name, base_salary, comission): |
| 32 | + super().__init__(name, base_salary) |
| 33 | + self.comission = comission |
| 34 | + |
| 35 | + def calculate_salary(self): |
| 36 | + return self.base_salary+self.comission |
| 37 | + |
| 38 | + |
| 39 | +empleado = Employee("Aldroide", 3000) |
| 40 | +empleado_tiempo_parcial = Part_time_employee("Emmanuel", 15, 80) |
| 41 | +empleado_con_comision = Comissioned_employee("Samira", 2500, 500) |
| 42 | + |
| 43 | +print(f"Salario de {empleado.name}: {empleado.calculate_salary()}") |
| 44 | +print( |
| 45 | + f"Salario de {empleado_tiempo_parcial.name}: {empleado_tiempo_parcial.calculate_salary()}") |
| 46 | +print( |
| 47 | + f"Salario de {empleado_con_comision.name}: {empleado_con_comision.calculate_salary()}") |
| 48 | + |
| 49 | + |
| 50 | +""" |
| 51 | + Desarrolla una calculadora que necesita realizar diversas operaciones matemáticas. |
| 52 | + Requisitos: |
| 53 | + - Debes diseñar un sistema que permita agregar nuevas operaciones utilizando el OCP. |
| 54 | + Instrucciones: |
| 55 | + 1. Implementa las operaciones de suma, resta, multiplicación y división. |
| 56 | + 2. Comprueba que el sistema funciona. |
| 57 | + 3. Agrega una quinta operación para calcular potencias. |
| 58 | + 4. Comprueba que se cumple el OCP. |
| 59 | +""" |
| 60 | + |
| 61 | + |
| 62 | +class Operation(ABC): |
| 63 | + @abstractmethod |
| 64 | + def calculate(self, a, b): |
| 65 | + pass |
| 66 | + |
| 67 | + |
| 68 | +class Add(Operation): |
| 69 | + def calculate(self, a, b): |
| 70 | + return a + b |
| 71 | + |
| 72 | + |
| 73 | +class Subtraction(Operation): |
| 74 | + def calculate(self, a, b): |
| 75 | + return a - b |
| 76 | + |
| 77 | + |
| 78 | +class Multiplication(Operation): |
| 79 | + def calculate(self, a, b): |
| 80 | + return a * b |
| 81 | + |
| 82 | + |
| 83 | +class Division(Operation): |
| 84 | + def calculate(self, a, b): |
| 85 | + if b == 0: |
| 86 | + raise ValueError("No se puede dividir por cero") |
| 87 | + return a / b |
| 88 | + |
| 89 | + |
| 90 | +class Power(Operation): |
| 91 | + def calculate(self, a, b): |
| 92 | + return a ** b |
| 93 | + |
| 94 | + |
| 95 | +class Calculator: |
| 96 | + def __init__(self): |
| 97 | + self.operations = {} |
| 98 | + |
| 99 | + def add_operation(self, name, operation): |
| 100 | + if not issubclass(type(operation), Operation): |
| 101 | + raise TypeError( |
| 102 | + "La operacion debe ser una estancia de la clase operación") |
| 103 | + self.operations[name] = operation |
| 104 | + |
| 105 | + def calculate(self, name, a, b): |
| 106 | + if name not in self.operations: |
| 107 | + raise ValueError(f"Operacion {name} no econtrada") |
| 108 | + return self.operations[name].calculate(a, b) |
| 109 | + |
| 110 | + |
| 111 | +calculadora = Calculator() |
| 112 | +calculadora.add_operation('suma', Add()) |
| 113 | +calculadora.add_operation('resta', Subtraction()) |
| 114 | +calculadora.add_operation('multiplicacion', Multiplication()) |
| 115 | +calculadora.add_operation('division', Division()) |
| 116 | +calculadora.add_operation('potencia', Power()) |
| 117 | + |
| 118 | + |
| 119 | +print(calculadora.calculate('suma', 5, 3)) # 8 |
| 120 | +print(calculadora.calculate('resta', 5, 3)) # 2 |
| 121 | +print(calculadora.calculate('multiplicacion', 5, 3)) # 15 |
| 122 | +print(calculadora.calculate('division', 5, 3)) # 1.666... |
| 123 | +print(calculadora.calculate('potencia', 5, 3)) # 125 |
0 commit comments