|
| 1 | + |
| 2 | +""" |
| 3 | +
|
| 4 | + * EJERCICIO: |
| 5 | + * Explora el "Principio SOLID Abierto-Cerrado (Open-Close Principle, OCP)" |
| 6 | + * y crea un ejemplo simple donde se muestre su funcionamiento |
| 7 | + * de forma correcta e incorrecta. |
| 8 | +""" |
| 9 | + |
| 10 | +# Forma incorrecta |
| 11 | + |
| 12 | +class Product(): |
| 13 | + def __init__(self, name) -> None: |
| 14 | + self.name = name |
| 15 | + |
| 16 | + def update_stock(self): |
| 17 | + if self.name == "zapatos": |
| 18 | + print("Actualizado el stock de zapatos") |
| 19 | + elif self.name == "camisas": |
| 20 | + print("Actualizado el stock de camisas") |
| 21 | + |
| 22 | +# Forma correcta |
| 23 | + |
| 24 | +from abc import ABC, abstractmethod |
| 25 | + |
| 26 | +class Product(ABC): |
| 27 | + @abstractmethod |
| 28 | + def update_stock(self): |
| 29 | + pass |
| 30 | + |
| 31 | +class Shoes(Product): |
| 32 | + |
| 33 | + def update_stock(self): |
| 34 | + print("Actualizado el stock de zapatos") |
| 35 | + |
| 36 | +class Shirts(Product): |
| 37 | + |
| 38 | + def update_stock(self): |
| 39 | + print("Actualizado el stock de camisas") |
| 40 | + |
| 41 | + |
| 42 | +my_products = [Shoes(), Shirts()] |
| 43 | + |
| 44 | +for i in my_products: |
| 45 | + i.update_stock() |
| 46 | + |
| 47 | +""" |
| 48 | + * DIFICULTAD EXTRA (opcional): |
| 49 | + * Desarrolla una calculadora que necesita realizar diversas operaciones matemáticas. |
| 50 | + * Requisitos: |
| 51 | + * - Debes diseñar un sistema que permita agregar nuevas operaciones utilizando el OCP. |
| 52 | + * Instrucciones: |
| 53 | + * 1. Implementa las operaciones de suma, resta, multiplicación y división. |
| 54 | + * 2. Comprueba que el sistema funciona. |
| 55 | + * 3. Agrega una quinta operación para calcular potencias. |
| 56 | + * 4. Comprueba que se cumple el OCP. |
| 57 | +
|
| 58 | +""" |
| 59 | +from abc import ABC, abstractmethod |
| 60 | + |
| 61 | +class Calculator(ABC): |
| 62 | + |
| 63 | + @abstractmethod |
| 64 | + def calculate_operation(self, a, b): |
| 65 | + pass |
| 66 | + |
| 67 | +class Sum(Calculator): |
| 68 | + |
| 69 | + def calculate_operation(self, a, b): |
| 70 | + return a + b |
| 71 | + |
| 72 | +class Subtract(Calculator): |
| 73 | + |
| 74 | + def calculate_operation(self, a, b): |
| 75 | + return a - b |
| 76 | + |
| 77 | +class Multiply(Calculator): |
| 78 | + |
| 79 | + def calculate_operation(self, a, b): |
| 80 | + return a * b |
| 81 | + |
| 82 | +class Division(Calculator): |
| 83 | + def calculate_operation(self, a, b): |
| 84 | + return a / b |
| 85 | + |
| 86 | +class Pow(Calculator): |
| 87 | + |
| 88 | + def calculate_operation(self, a, b): |
| 89 | + return a**b |
| 90 | + |
| 91 | + |
| 92 | +sum_op = Sum() |
| 93 | +substract_op = Subtract() |
| 94 | +multiply_op = Multiply() |
| 95 | +pow_op = Pow() |
| 96 | +division_op = Division() |
| 97 | + |
| 98 | +print(sum_op.calculate_operation(4, 5)) |
| 99 | +print(substract_op.calculate_operation(4,5)) |
| 100 | +print(multiply_op.calculate_operation(4, 5)) |
| 101 | +print(pow_op.calculate_operation(4,5)) |
| 102 | +print(division_op.calculate_operation(4, 5)) |
0 commit comments