Skip to content

Commit 35867f6

Browse files
authored
Merge pull request mouredev#7084 from caterinarodriguezdev/30-javascript
mouredev#30-javascript
2 parents 00df525 + 6838e31 commit 35867f6

File tree

1 file changed

+119
-0
lines changed

1 file changed

+119
-0
lines changed
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
/*
2+
* EJERCICIO:
3+
* Explora el "Principio SOLID de Inversión de Dependencias (Dependency Inversion
4+
* Principle, DIP)" y crea un ejemplo simple donde se muestre su funcionamiento
5+
* de forma correcta e incorrecta.
6+
*
7+
* DIFICULTAD EXTRA (opcional):
8+
* Crea un sistema de notificaciones.
9+
* Requisitos:
10+
* 1. El sistema puede enviar Email, PUSH y SMS (implementaciones específicas).
11+
* 2. El sistema de notificaciones no puede depender de las implementaciones específicas.
12+
* Instrucciones:
13+
* 1. Crea la interfaz o clase abstracta.
14+
* 2. Desarrolla las implementaciones específicas.
15+
* 3. Crea el sistema de notificaciones usando el DIP.
16+
* 4. Desarrolla un código que compruebe que se cumple el principio.
17+
*/
18+
19+
// INCORRECTO
20+
class Efectivo {
21+
cobrar() {
22+
console.log('Cobrando en efectivo');
23+
}
24+
}
25+
26+
class Caja {
27+
constructor() {
28+
this.efectivo = new Efectivo();
29+
}
30+
31+
cobrar() {
32+
this.efectivo.cobrar();
33+
}
34+
}
35+
36+
// CORRECTO
37+
class ICobro {
38+
cobrar() {
39+
throw Error('Debe implementar el método cobrar');
40+
}
41+
}
42+
43+
class EfectivoDIP extends ICobro {
44+
cobrar() {
45+
console.log('Cobrando en efectivo');
46+
}
47+
}
48+
49+
class TarjetaDIP extends ICobro {
50+
cobrar() {
51+
console.log('Cobrando con tarjeta');
52+
}
53+
}
54+
55+
class CajaDIP {
56+
constructor(service) {
57+
this.service = service;
58+
}
59+
60+
cobrar() {
61+
this.service.cobrar();
62+
}
63+
}
64+
65+
const efectivo = new EfectivoDIP();
66+
const tarjeta = new TarjetaDIP();
67+
const cajaE = new CajaDIP(efectivo);
68+
const cajaT = new CajaDIP(tarjeta);
69+
cajaE.cobrar();
70+
cajaT.cobrar();
71+
72+
console.log('-----------DIFICULTAD EXTREMA------');
73+
74+
class IMessage {
75+
send() {
76+
throw Error('Debe implementar el método send');
77+
}
78+
}
79+
80+
class SMS extends IMessage {
81+
send() {
82+
console.log('Mandando SMS');
83+
}
84+
}
85+
86+
class Push extends IMessage {
87+
send() {
88+
console.log('Mandando PUSH');
89+
}
90+
}
91+
92+
class Email extends IMessage {
93+
send() {
94+
console.log('Mandando email');
95+
}
96+
}
97+
98+
class Notification {
99+
constructor(service) {
100+
this.service = service;
101+
}
102+
103+
send() {
104+
this.service.send();
105+
}
106+
}
107+
108+
const sms = new SMS();
109+
const push = new Push();
110+
const email = new Email();
111+
112+
const notificationSMS = new Notification(sms);
113+
notificationSMS.send();
114+
115+
const notificationPush = new Notification(push);
116+
notificationPush.send();
117+
118+
const notificationEmail = new Notification(email);
119+
notificationEmail.send();

0 commit comments

Comments
 (0)