forked from mouredev/roadmap-retos-programacion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
65 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* EJERCICIO: | ||
* Explora el "Principio SOLID Abierto-Cerrado (Open-Close Principle, OCP)" | ||
* y crea un ejemplo simple donde se muestre su funcionamiento | ||
* de forma correcta e incorrecta. | ||
* | ||
* DIFICULTAD EXTRA (opcional): | ||
* Desarrolla una calculadora que necesita realizar diversas operaciones matemáticas. | ||
* Requisitos: | ||
* - Debes diseñar un sistema que permita agregar nuevas operaciones utilizando el OCP. | ||
* Instrucciones: | ||
* 1. Implementa las operaciones de suma, resta, multiplicación y división. | ||
* 2. Comprueba que el sistema funciona. | ||
* 3. Agrega una quinta operación para calcular potencias. | ||
* 4. Comprueba que se cumple el OCP. | ||
*/ | ||
|
||
//EJERCICIO | ||
/* | ||
PRINCIPIO ABIERTO-CERRADO: | ||
"Las entidades de software (clases, módulos, funciones, etc.) deben estar abiertas para su extensión, pero cerradas para su modificación" | ||
*/ | ||
|
||
//EXTRA | ||
class Calculator { | ||
display(operation, a, b) { | ||
const operationOptions = { | ||
add: this.add, | ||
susbtrac: this.susbtrac, | ||
multiplicate: this.multiplicate, | ||
divide: this.divide, | ||
}; | ||
|
||
function defaultfun() { | ||
console.log('Operacion no soportada o inexistente'); | ||
} | ||
|
||
const fun = operationOptions[operation] || defaultfun; | ||
|
||
fun(a, b); | ||
} | ||
|
||
add(a, b) { | ||
console.log(a + b); | ||
} | ||
|
||
susbtrac(a, b) { | ||
console.log(a - b); | ||
} | ||
|
||
multiplicate(a, b) { | ||
console.log(a * b); | ||
} | ||
|
||
divide(a, b) { | ||
console.log(a / b); | ||
} | ||
} | ||
|
||
const calculator = new Calculator(); | ||
|
||
calculator.display('add', 12, 10); | ||
calculator.display('potencia', 12, 10); |