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
+
7
+ // Forma incorrecta
8
+ /*
9
+ class Animal {
10
+ fly() {
11
+ console.log("Volar")
12
+ }
13
+
14
+ swim() {
15
+ console.log("Nadar")
16
+ }
17
+
18
+ walk() {
19
+ console.log("Caminar")
20
+ }
21
+ }
22
+
23
+ class Bird extends Animal {
24
+ fly() {
25
+ console.log("Volar")
26
+ }
27
+ }
28
+
29
+ class Fish extends Animal {
30
+ swim() {
31
+ console.log("Nadar")
32
+ }
33
+ }
34
+
35
+ class Dog extends Animal {
36
+ walk() {
37
+ console.log("Caminar")
38
+ }
39
+ fly() {
40
+ throw new Error("No puede volar")
41
+ }
42
+ }
43
+
44
+ const bird = new Bird()
45
+ bird.fly()
46
+
47
+ const fish = new Fish()
48
+ fish.swim()
49
+
50
+ const dog = new Dog()
51
+ dog.walk()
52
+
53
+ dog.fly()
54
+ */
55
+
56
+ // Forma correcta
57
+
58
+ class AnimalCanFly {
59
+ fly ( ) {
60
+ console . log ( "Volar" )
61
+ }
62
+ }
63
+
64
+ class AnimalCanSwim {
65
+ swim ( ) {
66
+ console . log ( "Nadar" )
67
+ }
68
+ }
69
+
70
+ class AnimalCanWalk {
71
+ walk ( ) {
72
+ console . log ( "Caminar" )
73
+ }
74
+ }
75
+
76
+ class Bird extends AnimalCanFly {
77
+ fly ( ) {
78
+ console . log ( "Volar" )
79
+ }
80
+ }
81
+
82
+ class Fish extends AnimalCanSwim {
83
+ swim ( ) {
84
+ console . log ( "Nadar" )
85
+ }
86
+ }
87
+
88
+ class Dog extends AnimalCanWalk {
89
+ walk ( ) {
90
+ console . log ( "Caminar" )
91
+ }
92
+ }
93
+
94
+ const bird = new Bird ( )
95
+ const fish = new Fish ( )
96
+ const dog = new Dog ( )
97
+
98
+ bird . fly ( )
99
+ fish . swim ( )
100
+ dog . walk ( )
101
+
102
+ /*
103
+ * DIFICULTAD EXTRA (opcional):
104
+ * Crea un gestor de impresoras.
105
+ * Requisitos:
106
+ * 1. Algunas impresoras sólo imprimen en blanco y negro.
107
+ * 2. Otras sólo a color.
108
+ * 3. Otras son multifunción, pueden imprimir, escanear y enviar fax.
109
+ * Instrucciones:
110
+ * 1. Implementa el sistema, con los diferentes tipos de impresoras y funciones.
111
+ * 2. Aplica el ISP a la implementación.
112
+ * 3. Desarrolla un código que compruebe que se cumple el principio.
113
+ */
114
+
115
+ class Printable {
116
+ print ( document ) {
117
+ console . log ( `Printing ${ document } ` )
118
+ }
119
+ }
120
+
121
+ class Scannable {
122
+ scan ( document ) {
123
+ console . log ( `Scanning ${ document } ` )
124
+ }
125
+ }
126
+
127
+ class Faxable {
128
+ sendFax ( document ) {
129
+ console . log ( `Sending ${ document } ` )
130
+ }
131
+ }
132
+
133
+ class BlackAndWhitePrinter extends Printable { }
134
+ class ColorPrinter extends Printable { }
135
+ class MultifunctionalPrinter extends Printable {
136
+ constructor ( ) {
137
+ super ( )
138
+ this . scanner = new Scannable ( )
139
+ this . fax = new Faxable ( )
140
+ }
141
+
142
+ scan ( document ) {
143
+ this . scanner . scan ( document )
144
+ }
145
+
146
+ sendFax ( document ) {
147
+ this . fax . sendFax ( document )
148
+ }
149
+ }
150
+
151
+ const bwPrinter = new BlackAndWhitePrinter ( )
152
+ bwPrinter . print ( "Document1" )
153
+
154
+ const colorPrinter = new ColorPrinter ( )
155
+ colorPrinter . print ( "Document2" )
156
+
157
+ const multiPrinter = new MultifunctionalPrinter ( )
158
+ multiPrinter . print ( "Document3" )
159
+ multiPrinter . scan ( "Document3" )
160
+ multiPrinter . sendFax ( "Document3" )
0 commit comments