1+ /** #27 - JavaScript -> Jesus Antonio Escamilla */
2+
3+ /**
4+ * El principio abierto-cerrado dice que el código debe estar abierto a la ampliación,
5+ pero cerrado a la modificación.
6+ * Lo que esto significa es que si queremos añadir funcionalidad adicional, deberíamos
7+ poder hacerlo simplemente ampliando la funcionalidad original, sin necesidad de modificarla.
8+ * El Principio de Abierto/Cerrado (Open/Closed Principle, OCP) establece que las entidades de software
9+ (como clases, módulos, funciones, etc.) deben estar abiertas para extensión, pero cerradas para modificación.
10+ * Esto significa que debemos ser capaces de agregar nuevo comportamiento al código existente sin modificarlo.
11+ */
12+
13+ //---EJERCIÓ---
14+ // INCORRECTO
15+ class Employee__ {
16+ constructor ( nombre , tipo ) {
17+ this . nombre = nombre ;
18+ this . tipo = tipo ;
19+ }
20+
21+ calculatePay ( ) {
22+ if ( this . tipo === 'fulltime' ) {
23+ return 4000 ;
24+ } else if ( this . tipo === 'parttime' ) {
25+ return 2000 ;
26+ } else if ( this . tipo === 'intern' ) {
27+ return 1000 ;
28+ }
29+ // Si agregamos un nuevo tipo, tenemos que modificar este método
30+ }
31+ }
32+
33+ // Ejemplo de uso de forma Incorrecta
34+ const fulltimeEmployee__ = new Employee__ ( 'Lizette' , 'fulltime' ) ;
35+ console . log ( fulltimeEmployee__ . calculatePay ( ) ) ;
36+
37+ const partTimeEmployee__ = new Employee__ ( 'Antonio' , 'parttime' ) ;
38+ console . log ( partTimeEmployee__ . calculatePay ( ) ) ;
39+
40+
41+ // CORRECTO
42+ class Employee {
43+ constructor ( nombre ) {
44+ this . nombre = nombre ;
45+ }
46+
47+ calculatePay ( ) {
48+ throw new Error ( 'You have implement the method calculatePay' ) ;
49+ }
50+ }
51+
52+ class FullTimeEmployee extends Employee {
53+ calculatePay ( ) {
54+ return 4000 ;
55+ }
56+ }
57+
58+ class PartTimeEmployee extends Employee {
59+ calculatePay ( ) {
60+ return 2000 ;
61+ }
62+ }
63+
64+ class InternEmployee extends Employee {
65+ calculatePay ( ) {
66+ return 1000 ;
67+ }
68+ }
69+
70+ // Ejemplo de uso de forma Correcta
71+ const fulltimeEmployee = new FullTimeEmployee ( 'Jesus' ) ;
72+ console . log ( fulltimeEmployee . calculatePay ( ) ) ;
73+
74+ const partTimeEmployee = new PartTimeEmployee ( 'Fatima' ) ;
75+ console . log ( partTimeEmployee . calculatePay ( ) ) ;
76+
77+ const internEmployee = new InternEmployee ( 'Enrique' ) ;
78+ console . log ( internEmployee . calculatePay ( ) ) ;
79+
80+ // Si necesitamos agregar un nuevo tipo de empleado, simplemente creamos una nueva clase que herede de Employee.
81+ class ContractorEmployee extends Employee {
82+ calculatePay ( ) {
83+ return 3000 ;
84+ }
85+ }
86+
87+ const contractorEmployee = new ContractorEmployee ( 'Mari' ) ;
88+ console . log ( contractorEmployee . calculatePay ( ) ) ;
89+
90+
91+ /**-----DIFICULTAD EXTRA-----*/
92+
93+ // Pendiente
94+
95+ /**-----DIFICULTAD EXTRA-----*/
0 commit comments