1
+ //#29 { retosparaprogramadores } - Principio SOLID de Segregación de Interfaces (Interface Segregation Principle (ISP))
2
+ /*
3
+ * EJERCICIO:
4
+ * Explora el "Principio SOLID de Segregación de Interfaces (Interface Segregation Principle, ISP)", y crea un ejemplo
5
+ * simple donde se muestre su funcionamiento de forma correcta e incorrecta.
6
+ *
7
+ * DIFICULTAD EXTRA (opcional):
8
+ * Crea un gestor de impresoras.
9
+ * Requisitos:
10
+ * 1. Algunas impresoras sólo imprimen en blanco y negro.
11
+ * 2. Otras sólo a color.
12
+ * 3. Otras son multifunción, pueden imprimir, escanear y enviar fax.
13
+ * Instrucciones:
14
+ * 1. Implementa el sistema, con los diferentes tipos de impresoras y funciones.
15
+ * 2. Aplica el ISP a la implementación.
16
+ * 3. Desarrolla un código que compruebe que se cumple el principio.
17
+ */
18
+ //Bibliografy: The Web Development Glossary (Jens Oliver Meiert) (Z-Library)
19
+ //GPT
20
+
21
+ /* Interface Segregation Principle
22
+ The principle that no client should be forced to depend on methods it does
23
+ not use. ISP splits interfaces that are very large into smaller and more
24
+ specific ones so that clients will only have to know about the methods that
25
+ are of interest to them. Such shrunken interfaces are also called role
26
+ interfaces. ISP is intended to keep a system decoupled and thus easier to
27
+ refactor, change, and redeploy. */
28
+
29
+ let log = console . log ;
30
+
31
+ // Check if running in a browser environment
32
+ const isBrowser = typeof window !== 'undefined' ;
33
+
34
+ // Conditional check for browser environment
35
+ if ( isBrowser ) {
36
+ window . addEventListener ( 'load' , ( ) => {
37
+ const body : HTMLBodyElement | null = document . querySelector ( 'body' ) ;
38
+ const title = document . createElement ( 'h1' ) ;
39
+
40
+ body ?. style . setProperty ( 'background' , '#000' ) ;
41
+ body ?. style . setProperty ( 'text-align' , 'center' ) ;
42
+
43
+ title . textContent = 'Retosparaprogramadores #28.' ;
44
+ title . style . setProperty ( 'font-size' , '3.5vmax' ) ;
45
+ title . style . setProperty ( 'color' , '#fff' ) ;
46
+ title . style . setProperty ( 'line-height' , '100vh' ) ;
47
+
48
+ body ?. appendChild ( title ) ;
49
+
50
+ setTimeout ( ( ) => {
51
+ alert ( 'Retosparaprogramadores #28. Please open the Browser Developer Tools.' ) ;
52
+ } , 2000 ) ;
53
+ log ( 'Retosparaprogramadores #28' ) ;
54
+ } ) ;
55
+ } else {
56
+ log ( 'This code is designed to run in a browser environment. Skipping window-related code.' ) ;
57
+ log ( 'Retosparaprogramadores #28' ) ;
58
+ }
59
+
60
+ // Incorrect Example
61
+ class PaymentService {
62
+ processCreditCardPayment ( amount : number ) : void { }
63
+ processPayPalPayment ( amount : number ) : void { }
64
+ processBitcoinPayment ( amount : number ) : void { }
65
+ }
66
+
67
+ class CreditCardPayment extends PaymentService {
68
+ processCreditCardPayment ( amount : number ) : void {
69
+ log ( `Processing credit card payment of ${ amount } ` ) ;
70
+ }
71
+
72
+ processPayPalPayment ( amount : number ) : void {
73
+ throw new Error ( "This payment method does not support PayPal payments" ) ;
74
+ }
75
+
76
+ processBitcoinPayment ( amount : number ) : void {
77
+ throw new Error ( "This payment method does not support Bitcoin payments" ) ;
78
+ }
79
+ }
80
+
81
+ class PayPalPayment extends PaymentService {
82
+ processCreditCardPayment ( amount : number ) : void {
83
+ throw new Error ( "This payment method does not support credit card payments" ) ;
84
+ }
85
+
86
+ processPayPalPayment ( amount : number ) : void {
87
+ log ( `Processing PayPal payment of ${ amount } ` ) ;
88
+ }
89
+
90
+ processBitcoinPayment ( amount : number ) : void {
91
+ throw new Error ( "This payment method does not support Bitcoin payments" ) ;
92
+ }
93
+ }
94
+
95
+ class BitcoinPayment extends PaymentService {
96
+ processCreditCardPayment ( amount : number ) : void {
97
+ throw new Error ( "This payment method does not support credit card payments" ) ;
98
+ }
99
+
100
+ processPayPalPayment ( amount : number ) : void {
101
+ throw new Error ( "This payment method does not support PayPal payments" ) ;
102
+ }
103
+
104
+ processBitcoinPayment ( amount : number ) : void {
105
+ log ( `Processing Bitcoin payment of ${ amount } ` ) ;
106
+ }
107
+ }
108
+
109
+
110
+ const creditCardPayment = new CreditCardPayment ( ) ;
111
+ creditCardPayment . processCreditCardPayment ( 250 ) ; // Processing credit card payment of 250
112
+ //creditCardPayment.processPayPalPayment(87); // This will throw an error
113
+ //Error: This payment method does not support PayPal payments at CreditCardPayment.processPayPalPayment
114
+
115
+ // Correct Example
116
+ class CreditCardPaymentService {
117
+ processCreditCardPayment ( amount : number ) : void {
118
+ log ( `Processing credit card payment of ${ amount } ` ) ;
119
+ }
120
+ }
121
+
122
+ class PayPalPaymentService {
123
+ processPayPalPayment ( amount : number ) : void {
124
+ log ( `Processing PayPal payment of ${ amount } ` ) ;
125
+ }
126
+ }
127
+
128
+ class BitcoinPaymentService {
129
+ processBitcoinPayment ( amount : number ) : void {
130
+ log ( `Processing Bitcoin payment of ${ amount } ` ) ;
131
+ }
132
+ }
133
+
134
+
135
+ const creditCardPayment1 = new CreditCardPaymentService ( ) ;
136
+ creditCardPayment1 . processCreditCardPayment ( 400 ) ; // Processing credit card payment of 400
137
+
138
+ const payPalPayment = new PayPalPaymentService ( ) ;
139
+ payPalPayment . processPayPalPayment ( 130 ) ; // Processing PayPal payment of 130
140
+
141
+ const bitcoinPayment = new BitcoinPaymentService ( ) ;
142
+ bitcoinPayment . processBitcoinPayment ( 0.020 ) ; // Processing Bitcoin payment of 0.02
143
+
144
+
145
+ //Extra Dificulty Exercise
146
+
147
+ class BlackAndWhitePrinter {
148
+ print ( doc : string ) : void {
149
+ log ( `Printing: ${ doc } in Black & White` ) ;
150
+ }
151
+ }
152
+
153
+ class ColorPrinter {
154
+ print ( doc : string ) : void {
155
+ log ( `Printing: ${ doc } in Color` ) ;
156
+ }
157
+ }
158
+
159
+ class MultiFunctionPrinter {
160
+
161
+ printInBlackAndWhite ( doc : string ) : void {
162
+ log ( `Printing: ${ doc } in Black & White` ) ;
163
+ }
164
+
165
+ print ( doc : string ) : void {
166
+ log ( `Printing: ${ doc } in Color` ) ;
167
+ }
168
+
169
+ fax ( doc : string ) : void {
170
+ log ( `Faxing: ${ doc } ` ) ;
171
+ }
172
+
173
+ scan ( doc : string ) : void {
174
+ log ( `Scanning: ${ doc } ` ) ;
175
+ }
176
+ }
177
+
178
+ let book = 'vuelointemporal.odt' ;
179
+
180
+ const bw_printer = new BlackAndWhitePrinter ( ) ;
181
+ bw_printer . print ( book ) ; // Printing: vuelointemporal.odt in Black & White
182
+
183
+ const c_printer = new ColorPrinter ( ) ;
184
+ c_printer . print ( book ) ; // vuelointemporal.odt in Color
185
+
186
+ const m_printer = new MultiFunctionPrinter ( ) ;
187
+ m_printer . printInBlackAndWhite ( book ) ; // vuelointemporal.odt in Black & White
188
+ m_printer . print ( book ) ; // Printing: vuelointemporal.odt in Color
189
+ m_printer . fax ( book ) ; // Faxing: vuelointemporal.odt
190
+ m_printer . scan ( book ) ; // Scanning: vuelointemporal.odt
0 commit comments