Skip to content

Commit 32ff64e

Browse files
#29 - python
1 parent c9fe7d1 commit 32ff64e

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
#29 { Retos para Programadores } Principio SOLID de Segregación de Interfaces (Interface Segregation Principle, ISP)
2+
3+
# Bibliography reference:
4+
# The Web Development Glossary (Jens Oliver Meiert) (Z-Library)
5+
# I use GPT as a reference and sometimes to correct or generate proper comments.
6+
7+
"""
8+
* EJERCICIO:
9+
* Explora el "Principio SOLID de Segregación de Interfaces
10+
* (Interface Segregation Principle, ISP)", y crea un ejemplo
11+
* simple donde se muestre su funcionamiento de forma correcta e incorrecta.
12+
*
13+
* DIFICULTAD EXTRA (opcional):
14+
* Crea un gestor de impresoras.
15+
* Requisitos:
16+
* 1. Algunas impresoras sólo imprimen en blanco y negro.
17+
* 2. Otras sólo a color.
18+
* 3. Otras son multifunción, pueden imprimir, escanear y enviar fax.
19+
* Instrucciones:
20+
* 1. Implementa el sistema, con los diferentes tipos de impresoras y funciones.
21+
* 2. Aplica el ISP a la implementación.
22+
* 3. Desarrolla un código que compruebe que se cumple el principio.
23+
"""
24+
25+
""" Interface Segregation Principle
26+
The principle that no client should be forced to depend on methods it does
27+
not use. ISP splits interfaces that are very large into smaller and more
28+
specific ones so that clients will only have to know about the methods that
29+
are of interest to them. Such shrunken interfaces are also called role
30+
interfaces. ISP is intended to keep a system decoupled and thus easier to
31+
refactor, change, and redeploy. """
32+
33+
34+
# Shorthan for print
35+
log = print
36+
37+
# Incorrect Example
38+
39+
class PaymentService:
40+
def process_credit_card_payment(self, amount):
41+
pass
42+
43+
def process_paypal_payment(self, amount):
44+
pass
45+
46+
def process_bitcoin_payment(self, amount):
47+
pass
48+
49+
50+
class CreditCardPayment(PaymentService):
51+
def process_credit_card_payment(self, amount):
52+
log(f"Processing credit card payment of {amount}")
53+
54+
def process_paypal_payment(self, amount):
55+
raise Exception("This payment method does not support PayPal payments")
56+
57+
def process_bitcoin_payment(self, amount):
58+
raise Exception("This payment method does not support Bitcoin payments")
59+
60+
61+
class PayPalPayment(PaymentService):
62+
def process_credit_card_payment(self, amount):
63+
raise Exception("This payment method does not support credit card payments")
64+
65+
def process_paypal_payment(self, amount):
66+
log(f"Processing PayPal payment of {amount}")
67+
68+
def process_bitcoin_payment(self, amount):
69+
raise Exception("This payment method does not support Bitcoin payments")
70+
71+
72+
class BitcoinPayment(PaymentService):
73+
def process_credit_card_payment(self, amount):
74+
raise Exception("This payment method does not support credit card payments")
75+
76+
def process_paypal_payment(self, amount):
77+
raise Exception("This payment method does not support PayPal payments")
78+
79+
def process_bitcoin_payment(self, amount):
80+
log(f"Processing Bitcoin payment of {amount}")
81+
82+
83+
# Example usage
84+
credit_card_payment = CreditCardPayment()
85+
credit_card_payment.process_credit_card_payment(250) # Processing credit card payment of 250
86+
# credit_card_payment.process_paypal_payment(87) # This will raise an exception
87+
# Exception: This payment method does not support PayPal payments
88+
89+
# Correct Example
90+
91+
class CreditCardPaymentService:
92+
def process_credit_card_payment(self, amount):
93+
print(f"Processing credit card payment of {amount}")
94+
95+
96+
class PayPalPaymentService:
97+
def process_paypal_payment(self, amount):
98+
print(f"Processing PayPal payment of {amount}")
99+
100+
101+
class BitcoinPaymentService:
102+
def process_bitcoin_payment(self, amount):
103+
print(f"Processing Bitcoin payment of {amount}")
104+
105+
106+
# Example usage
107+
credit_card_payment1 = CreditCardPaymentService()
108+
credit_card_payment1.process_credit_card_payment(400) # Processing credit card payment of 400
109+
110+
pay_pal_payment = PayPalPaymentService()
111+
pay_pal_payment.process_paypal_payment(130) # Processing PayPal payment of 130
112+
113+
bitcoin_payment = BitcoinPaymentService()
114+
bitcoin_payment.process_bitcoin_payment(0.020) # Processing Bitcoin payment of 0.02
115+
116+
117+
# Extra Difficulty Exercise
118+
119+
class BlackAndWhitePrinter:
120+
def print(self, doc):
121+
print(f"Printing: {doc} in Black & White")
122+
123+
124+
class ColorPrinter:
125+
def print(self, doc):
126+
print(f"Printing: {doc} in Color")
127+
128+
129+
class MultiFunctionPrinter:
130+
def print_in_black_and_white(self, doc):
131+
print(f"Printing: {doc} in Black & White")
132+
133+
def print(self, doc):
134+
print(f"Printing: {doc} in Color")
135+
136+
def fax(self, doc):
137+
print(f"Faxing: {doc}")
138+
139+
def scan(self, doc):
140+
print(f"Scanning: {doc}")
141+
142+
143+
# Example usage for printers
144+
book = 'vuelointemporal.odt'
145+
146+
bw_printer = BlackAndWhitePrinter()
147+
bw_printer.print(book) # Printing: vuelointemporal.odt in Black & White
148+
149+
c_printer = ColorPrinter()
150+
c_printer.print(book) # Printing: vuelointemporal.odt in Color
151+
152+
m_printer = MultiFunctionPrinter()
153+
m_printer.print_in_black_and_white(book) # Printing: vuelointemporal.odt in Black & White
154+
m_printer.print(book) # Printing: vuelointemporal.odt in Color
155+
m_printer.fax(book) # Faxing: vuelointemporal.odt
156+
m_printer.scan(book) # Scanning: vuelointemporal.odt

0 commit comments

Comments
 (0)