Skip to content

Commit e8cfc1a

Browse files
authored
Merge pull request mouredev#4938 from Kenysdev/29.py
#29 - python
2 parents 279cf54 + ff2160f commit e8cfc1a

File tree

1 file changed

+146
-0
lines changed

1 file changed

+146
-0
lines changed
+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
# ╔═════════════════════════════════════╗
2+
# ║ Autor: Kenys Alvarado ║
3+
# ║ GitHub: https://github.com/Kenysdev ║
4+
# ║ 2024 - Python ║
5+
# ╚═════════════════════════════════════╝
6+
7+
# -----------------------------------------------------
8+
# * SOLID: PRINCIPIO DE SEGREGACIÓN DE INTERFACES (ISP)
9+
# -----------------------------------------------------
10+
# - Una clase no debería estar obligada a implementar interfaces que no utiliza.
11+
# Evitando crear grandes clases monolíticas.
12+
13+
from abc import ABC, abstractmethod
14+
from decimal import Decimal
15+
16+
# NOTA: Este ejemplo muestra el uso CORRECTO. Para suponer un ejemplo que viole el principio, sería
17+
# imajinar todos los métodos Y propiedades siguientes, en una sola clase.
18+
19+
class Employe(ABC):
20+
def __init__(self, name: str) -> None:
21+
self._name = name
22+
23+
def get_name(self) -> str:
24+
return self._name
25+
26+
@abstractmethod
27+
def calculate_payment(self) -> Decimal:
28+
pass
29+
30+
class FullTime(Employe, ABC):
31+
def __init__(self, name: str) -> None:
32+
super().__init__(name)
33+
34+
@abstractmethod
35+
def assign_bonus(self) -> Decimal:
36+
#return Decimal('100.00')
37+
pass
38+
39+
class Hourly(Employe, ABC):
40+
def __init__(self, name: str, hours: float) -> None:
41+
super().__init__(name)
42+
self._hours = hours
43+
44+
def set_hours(self, hours: float):
45+
self._hours += hours
46+
47+
class Intern(Employe, ABC):
48+
def __init__(self, name: str, total_tickets: int, ticket_price: Decimal):
49+
super().__init__(name)
50+
self._total_tickets = total_tickets
51+
self._ticket_price = ticket_price
52+
53+
# _______________________________________
54+
# ejmp de uso 1
55+
class SellerIntern(Intern):
56+
def calculate_payment(self) -> Decimal:
57+
return self._total_tickets * Decimal(self._ticket_price)
58+
59+
seller_intern = SellerIntern("Zoe", 40, Decimal('5.10'))
60+
print(seller_intern.get_name())
61+
print(seller_intern.calculate_payment())
62+
63+
#________________________________________
64+
# ejmp de uso 2
65+
class SellerFullTime(FullTime):
66+
def assign_bonus(self) -> Decimal:
67+
return Decimal('50.00')
68+
69+
def calculate_payment(self) -> Decimal:
70+
return Decimal('1500.00') + self.assign_bonus()
71+
72+
seller = SellerFullTime("Ben")
73+
print(seller.get_name())
74+
print(seller.calculate_payment())
75+
76+
"""
77+
* EJERCICIO:
78+
* Crea un gestor de impresoras.
79+
* Requisitos:
80+
* 1. Algunas impresoras sólo imprimen en blanco y negro.
81+
* 2. Otras sólo a color.
82+
* 3. Otras son multifunción, pueden imprimir, escanear y enviar fax.
83+
* Instrucciones:
84+
* 1. Implementa el sistema, con los diferentes tipos de impresoras y funciones.
85+
* 2. Aplica el ISP a la implementación.
86+
* 3. Desarrolla un código que compruebe que se cumple el principio.
87+
"""
88+
89+
class Printer(ABC):
90+
@abstractmethod
91+
def print_file(self, file: str) -> None:
92+
pass
93+
94+
class Scanner:
95+
def to_scan(self, path_save: str) -> None:
96+
print("\nEscaneo realizado, Guardado en:", path_save)
97+
98+
class Fax:
99+
def send_file(self, file: str, phone_number: int) -> None:
100+
print("\n-", file, "Fue enviado a:", phone_number)
101+
102+
class MonoPrinter(Printer):
103+
def print_file(self, file: str) -> None:
104+
print("\nImpresora blanco y negro:")
105+
print(file, " se imprimió.")
106+
107+
class ColorPrinter(Printer):
108+
def print_file(self, file: str) -> None:
109+
print("\nImpresora a color:")
110+
print(file, " se imprimió.")
111+
112+
113+
class MultiFunctionPrinter:
114+
def __init__(self):
115+
self.mono_printer = MonoPrinter()
116+
self.color_printer = ColorPrinter()
117+
self.scanner = Scanner()
118+
self.fax = Fax()
119+
120+
#___________
121+
# blanco y negro.
122+
mono_printer = MonoPrinter()
123+
mono_printer.print_file("filex.pdf")
124+
125+
#___________
126+
color_printer = ColorPrinter()
127+
color_printer.print_file("filex.pdf")
128+
129+
#___________
130+
scaner = Scanner()
131+
scaner.to_scan("c:\\docs")
132+
133+
#___________
134+
fax = Fax()
135+
fax.send_file("filex.pdf", 12345678)
136+
137+
#___________
138+
print("\n___________\nMultifunción:")
139+
multifunction = MultiFunctionPrinter()
140+
141+
multifunction.mono_printer.print_file("filex.pdf")
142+
multifunction.color_printer.print_file("filex.pdf")
143+
multifunction.scanner.to_scan("c:\\docs")
144+
multifunction.fax.send_file("filex.pdf", 12345678)
145+
146+
# end

0 commit comments

Comments
 (0)