1+ //#29 - 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+ window . addEventListener ( 'load' , ( ) => {
32+ const body = document . querySelector ( 'body' ) ;
33+ const title = document . createElement ( 'h1' ) ;
34+
35+ body . style . setProperty ( 'background' , '#000' ) ;
36+ body . style . setProperty ( 'text-align' , 'center' ) ;
37+
38+ title . textContent = 'Retosparaprogramadores #29.' ;
39+ title . style . setProperty ( 'font-size' , '3.5vmax' ) ;
40+ title . style . setProperty ( 'color' , '#fff' ) ;
41+ title . style . setProperty ( 'line-height' , '100vh' ) ;
42+
43+ body . appendChild ( title ) ;
44+
45+ setTimeout ( ( ) => {
46+ alert ( 'Retosparaprogramadores #29. Please open the Browser Developer Tools.' ) ;
47+ } , 2000 ) ;
48+ log ( 'Retosparaprogramadores #29' ) ;
49+ } ) ;
50+
51+ // Incorrect Example
52+ class PaymentService {
53+ processCreditCardPayment ( amount ) { }
54+ processPayPalPayment ( amount ) { }
55+ processBitcoinPayment ( amount ) { }
56+ }
57+
58+ class CreditCardPayment extends PaymentService {
59+ processCreditCardPayment ( amount ) {
60+ log ( `Processing credit card payment of ${ amount } ` ) ;
61+ }
62+
63+ processPayPalPayment ( amount ) {
64+ throw new Error ( "This payment method does not support PayPal payments" ) ;
65+ }
66+
67+ processBitcoinPayment ( amount ) {
68+ throw new Error ( "This payment method does not support Bitcoin payments" ) ;
69+ }
70+ }
71+
72+ class PayPalPayment extends PaymentService {
73+ processCreditCardPayment ( amount ) {
74+ throw new Error ( "This payment method does not support credit card payments" ) ;
75+ }
76+
77+ processPayPalPayment ( amount ) {
78+ log ( `Processing PayPal payment of ${ amount } ` ) ;
79+ }
80+
81+ processBitcoinPayment ( amount ) {
82+ throw new Error ( "This payment method does not support Bitcoin payments" ) ;
83+ }
84+ }
85+
86+ class BitcoinPayment extends PaymentService {
87+ processCreditCardPayment ( amount ) {
88+ throw new Error ( "This payment method does not support credit card payments" ) ;
89+ }
90+
91+ processPayPalPayment ( amount ) {
92+ throw new Error ( "This payment method does not support PayPal payments" ) ;
93+ }
94+
95+ processBitcoinPayment ( amount ) {
96+ log ( `Processing Bitcoin payment of ${ amount } ` ) ;
97+ }
98+ }
99+
100+
101+ const creditCardPayment = new CreditCardPayment ( ) ;
102+ creditCardPayment . processCreditCardPayment ( 250 ) ; // Processing credit card payment of 250
103+ //creditCardPayment.processPayPalPayment(87); // This will throw an error
104+ //Error: This payment method does not support PayPal payments at CreditCardPayment.processPayPalPayment
105+
106+ // Correct Example
107+ class CreditCardPaymentService {
108+ processCreditCardPayment ( amount ) {
109+ log ( `Processing credit card payment of ${ amount } ` ) ;
110+ }
111+ }
112+
113+ class PayPalPaymentService {
114+ processPayPalPayment ( amount ) {
115+ log ( `Processing PayPal payment of ${ amount } ` ) ;
116+ }
117+ }
118+
119+ class BitcoinPaymentService {
120+ processBitcoinPayment ( amount ) {
121+ log ( `Processing Bitcoin payment of ${ amount } ` ) ;
122+ }
123+ }
124+
125+
126+ const creditCardPayment1 = new CreditCardPaymentService ( ) ;
127+ creditCardPayment1 . processCreditCardPayment ( 400 ) ; // Processing credit card payment of 400
128+
129+ const payPalPayment = new PayPalPaymentService ( ) ;
130+ payPalPayment . processPayPalPayment ( 130 ) ; // Processing PayPal payment of 130
131+
132+ const bitcoinPayment = new BitcoinPaymentService ( ) ;
133+ bitcoinPayment . processBitcoinPayment ( 0.020 ) ; // Processing Bitcoin payment of 0.02
134+
135+
136+ //Extra Dificulty Exercise
137+
138+ class BlackAndWhitePrinter {
139+ print ( doc ) {
140+ log ( `Printing: ${ doc } in Black & White` ) ;
141+ }
142+ }
143+
144+ class ColorPrinter {
145+ print ( doc ) {
146+ log ( `Printing: ${ doc } in Color` ) ;
147+ }
148+ }
149+
150+ class MultiFunctionPrinter {
151+
152+ printInBlackAndWhite ( doc ) {
153+ log ( `Printing: ${ doc } in Black & White` ) ;
154+ }
155+
156+ print ( doc ) {
157+ log ( `Printing: ${ doc } in Color` ) ;
158+ }
159+
160+ fax ( doc ) {
161+ log ( `Faxing: ${ doc } ` ) ;
162+ }
163+
164+ scan ( doc ) {
165+ log ( `Scanning: ${ doc } ` ) ;
166+ }
167+ }
168+
169+ let book = 'vuelointemporal.odt' ;
170+
171+ const bw_printer = new BlackAndWhitePrinter ( ) ;
172+ bw_printer . print ( book ) ; // Printing: vuelointemporal.odt in Black & White
173+
174+ const c_printer = new ColorPrinter ( ) ;
175+ c_printer . print ( book ) ; // vuelointemporal.odt in Color
176+
177+ const m_printer = new MultiFunctionPrinter ( ) ;
178+ m_printer . printInBlackAndWhite ( book ) ; // vuelointemporal.odt in Black & White
179+ m_printer . print ( book ) ; // Printing: vuelointemporal.odt in Color
180+ m_printer . fax ( book ) ; // Faxing: vuelointemporal.odt
181+ m_printer . scan ( book ) ; // Scanning: vuelointemporal.odt
0 commit comments