Skip to content

Commit beefd0f

Browse files
committed
29 - js - SOLID ISP
1 parent 7d07781 commit beefd0f

File tree

1 file changed

+149
-0
lines changed

1 file changed

+149
-0
lines changed
Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
_____________________________________
3+
https://github.com/kenysdev
4+
2024 - JavaScript
5+
___________________________________________________
6+
#29 SOLID: PRINCIPIO DE SEGREGACIÓN DE INTERFACES (ISP)
7+
---------------------------------------------------
8+
 * EJERCICIO:
9+
 * Explora el "Principio SOLID de Segregación de Interfaces (Interface Segregation Principle, ISP)"
10+
 * y crea un ejemplo simple donde se muestre su funcionamiento de forma correcta e incorrecta.
11+
 *
12+
 * DIFICULTAD EXTRA (opcional):
13+
 * Crea un gestor de impresoras.
14+
 * Requisitos:
15+
 * 1. Algunas impresoras sólo imprimen en blanco y negro.
16+
 * 2. Otras sólo a color.
17+
 * 3. Otras son multifunción, pueden imprimir, escanear y enviar fax.
18+
 * Instrucciones:
19+
 * 1. Implementa el sistema, con los diferentes tipos de impresoras y funciones.
20+
 * 2. Aplica el ISP a la implementación.
21+
 * 3. Desarrolla un código que compruebe que se cumple el principio.
22+
*/
23+
// ________________________________________________________
24+
// Abstracciones
25+
class AbsPlayable {
26+
play() {
27+
throw new Error("El método 'play' debe ser implementado.");
28+
}
29+
}
30+
31+
class AbsDisplayable {
32+
display() {
33+
throw new Error("El método 'display' debe ser implementado.");
34+
}
35+
}
36+
37+
//__________________________
38+
// Implementar abstracciones
39+
40+
class Speaker extends AbsPlayable {
41+
play() {
42+
console.log("El altavoz está reproduciendo música.");
43+
}
44+
}
45+
46+
class Phone extends AbsPlayable {
47+
play() {
48+
console.log("El teléfono está reproduciendo música.");
49+
}
50+
}
51+
52+
Object.assign(Phone.prototype, AbsDisplayable.prototype);
53+
54+
Phone.prototype.display = function() {
55+
console.log("El teléfono está mostrando la pantalla de reproducción.");
56+
};
57+
58+
//__________________________
59+
// Utilización
60+
const speaker = new Speaker();
61+
speaker.play();
62+
63+
const phone = new Phone();
64+
phone.play();
65+
phone.display();
66+
67+
// ________________________________________________________
68+
// DIFICULTAD EXTRA
69+
// ----------------
70+
71+
// Abstracciones
72+
class AbsPrinter {
73+
printFile(file) {
74+
throw new Error("El método 'printFile' debe ser implementado.");
75+
}
76+
}
77+
78+
class AbsScanner {
79+
toScan(pathSave) {
80+
throw new Error("El método 'toScan' debe ser implementado.");
81+
}
82+
}
83+
84+
class AbsFax {
85+
sendFile(file, phoneNumber) {
86+
throw new Error("El método 'sendFile' debe ser implementado.");
87+
}
88+
}
89+
90+
//__________________________
91+
// Implementar abstracciones
92+
class MonoPrinter extends AbsPrinter {
93+
printFile(file) {
94+
console.log("\nImpresora blanco y negro:");
95+
console.log(`${file} se imprimió.`);
96+
}
97+
}
98+
99+
class ColorPrinter extends AbsPrinter {
100+
printFile(file) {
101+
console.log("\nImpresora a color:");
102+
console.log(`${file} se imprimió.`);
103+
}
104+
}
105+
106+
class Scanner extends AbsScanner {
107+
toScan(pathSave) {
108+
console.log("\nEscaneo realizado, Guardado en:", pathSave);
109+
}
110+
}
111+
112+
class Fax extends AbsFax {
113+
sendFile(file, phoneNumber) {
114+
console.log(`\n- ${file} fue enviado a: ${phoneNumber}`);
115+
}
116+
}
117+
118+
class MultiFunctionPrinter {
119+
constructor() {
120+
this.monoPrinter = new MonoPrinter();
121+
this.colorPrinter = new ColorPrinter();
122+
this.scanner = new Scanner();
123+
this.fax = new Fax();
124+
}
125+
}
126+
127+
//__________________________
128+
// Utilización
129+
console.log("\nDIFICULTAD EXTRA");
130+
131+
const monoPrinter = new MonoPrinter();
132+
monoPrinter.printFile("filex.pdf");
133+
134+
const colorPrinter = new ColorPrinter();
135+
colorPrinter.printFile("filex.pdf");
136+
137+
const scanner = new Scanner();
138+
scanner.toScan("c:\\docs");
139+
140+
const fax = new Fax();
141+
fax.sendFile("filex.pdf", 12345678);
142+
143+
console.log("\n___________\nMultifunción:");
144+
const multifunction = new MultiFunctionPrinter();
145+
146+
multifunction.monoPrinter.printFile("filex.pdf");
147+
multifunction.colorPrinter.printFile("filex.pdf");
148+
multifunction.scanner.toScan("c:\\docs");
149+
multifunction.fax.sendFile("filex.pdf", 12345678);

0 commit comments

Comments
 (0)