1+ /*
2+ * EJERCICIO:
3+ * Explora el "Principio SOLID de Segregación de Interfaces (Interface Segregation Principle, ISP)"
4+ * y crea un ejemplo simple donde se muestre su funcionamiento de forma correcta e incorrecta.
5+ *
6+ * DIFICULTAD EXTRA (opcional):
7+ * Crea un gestor de impresoras.
8+ * Requisitos:
9+ * 1. Algunas impresoras sólo imprimen en blanco y negro.
10+ * 2. Otras sólo a color.
11+ * 3. Otras son multifunción, pueden imprimir, escanear y enviar fax.
12+ * Instrucciones:
13+ * 1. Implementa el sistema, con los diferentes tipos de impresoras y funciones.
14+ * 2. Aplica el ISP a la implementación.
15+ * 3. Desarrolla un código que compruebe que se cumple el principio.
16+ */
17+
18+
19+ // Incorrect
20+
21+ class WorkerInterface {
22+ work ( ) { }
23+ eat ( ) { }
24+ }
25+
26+
27+ class HumanWrong extends WorkerInterface {
28+ work ( ) { }
29+ eat ( ) { }
30+ }
31+
32+
33+ class RobotWrong extends WorkerInterface {
34+ work ( ) { }
35+ eat ( ) { /* Robots don't eat */ }
36+ }
37+
38+
39+ // Correct
40+
41+ class WorkInterface {
42+ work ( ) {
43+ console . log ( 'Working' ) ;
44+ }
45+ }
46+
47+ class EatInterface {
48+ eat ( ) {
49+ console . log ( 'Eating' ) ;
50+ }
51+ }
52+
53+
54+ class Human {
55+ constructor ( ) {
56+ // Extend from multiple classes
57+ this . workSuper = new WorkInterface ( )
58+ this . eatSuper = new EatInterface ( )
59+ }
60+
61+ work ( ) {
62+ this . workSuper . work ( )
63+ }
64+
65+ eat ( ) {
66+ this . eatSuper . eat ( )
67+ }
68+ }
69+
70+ class Robot extends WorkInterface {
71+ work ( ) {
72+ super . work ( )
73+ }
74+ }
75+
76+
77+ const human = new Human ( )
78+ human . work ( )
79+ human . eat ( )
80+
81+ const robot = new Robot ( )
82+ robot . work ( )
83+
84+
85+ /**
86+ * DIFICULTAD EXTRA (opcional):
87+ * Crea un gestor de impresoras.
88+ * Requisitos:
89+ * 1. Algunas impresoras sólo imprimen en blanco y negro.
90+ * 2. Otras sólo a color.
91+ * 3. Otras son multifunción, pueden imprimir, escanear y enviar fax.
92+ * Instrucciones:
93+ * 1. Implementa el sistema, con los diferentes tipos de impresoras y funciones.
94+ * 2. Aplica el ISP a la implementación.
95+ * 3. Desarrolla un código que compruebe que se cumple el principio.
96+ */
97+
98+
99+ class PrinterInterface {
100+ print ( document ) {
101+ console . log ( `Printing (black & white): ${ document } ` ) ;
102+ }
103+ }
104+
105+
106+ class ColorPrinterInterface {
107+ printColor ( document ) {
108+ console . log ( `Printing (color): ${ document } ` ) ;
109+ }
110+ }
111+
112+
113+ class ScannerInterface {
114+ scan ( document ) {
115+ console . log ( `Scanning document: ${ document } ` ) ;
116+ return 'scanned' + document
117+ }
118+ }
119+
120+
121+ class FaxInterface {
122+ sendFax ( document ) {
123+ console . log ( `Sending through fax: ${ document } ` ) ;
124+ }
125+ }
126+
127+
128+ class Printer extends PrinterInterface {
129+ print ( document ) {
130+ super . print ( document ) ;
131+ }
132+ }
133+
134+
135+ class ColorPrinter extends ColorPrinterInterface {
136+ printColor ( document ) {
137+ super . printColor ( document )
138+ }
139+ }
140+
141+
142+ class MultifunctionPrinter {
143+ // Private properties -> to use them only inside class
144+ #printBW
145+ #colorPrint
146+ #scanner
147+ #fax
148+
149+ constructor ( ) {
150+ this . #printBW = new PrinterInterface ( ) ;
151+ this . #colorPrint = new ColorPrinterInterface ( ) ;
152+ this . #scanner = new ScannerInterface ( ) ;
153+ this . #fax = new FaxInterface ( ) ;
154+ }
155+
156+ print ( document ) {
157+ this . #printBW. print ( document ) ;
158+ }
159+
160+ printColor ( document ) {
161+ this . #colorPrint. printColor ( document ) ;
162+ }
163+
164+ scan ( document ) {
165+ return this . #scanner. scan ( document ) ;
166+ }
167+
168+ sendFax ( document ) {
169+ this . #fax. sendFax ( document ) ;
170+ }
171+ }
172+
173+
174+ let doc = 'my-document.txt' ;
175+
176+ const printer = new Printer ( ) ;
177+ printer . print ( doc ) ;
178+
179+ const colorPrinter = new ColorPrinter ( ) ;
180+ colorPrinter . printColor ( doc ) ;
181+
182+
183+ doc = 'another-doc.txt' ;
184+
185+ const multifunctionPrinter = new MultifunctionPrinter ( ) ;
186+ multifunctionPrinter . print ( doc ) ;
187+ multifunctionPrinter . printColor ( doc ) ;
188+ multifunctionPrinter . scan ( doc ) ;
189+ multifunctionPrinter . sendFax ( doc ) ;
0 commit comments