forked from mouredev/roadmap-retos-programacion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhozlucas28.py
226 lines (155 loc) · 4.56 KB
/
hozlucas28.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# pylint: disable=missing-class-docstring,pointless-string-statement,missing-function-docstring,broad-exception-raised,too-few-public-methods,missing-module-docstring
from abc import ABCMeta, abstractmethod
"""
Interface Segregation Principle (ISP)...
"""
print("Interface Segregation Principle (ISP)...")
class AbcBadWorker(metaclass=ABCMeta):
@abstractmethod
def eat(self) -> None:
pass
@abstractmethod
def work(self) -> None:
pass
class BadProgrammer(AbcBadWorker):
def eat(self) -> None:
print("Eating!")
def work(self) -> None:
print("Working!")
class BadRobot(AbcBadWorker):
def eat(self) -> None:
raise Exception("A robot can not eat")
def work(self) -> None:
print("Working!")
print("\nBad implementation of Interface Segregation Principle (ISP)...")
print(
"\n```",
"""class AbcBadWorker(metaclass=ABCMeta):
@abstractmethod
def eat(self) -> None:
pass
@abstractmethod
def work(self) -> None:
pass
class BadProgrammer(AbcBadWorker):
def eat(self) -> None:
print("Eating!")
def work(self) -> None:
print("Working!")
class BadRobot(AbcBadWorker):
def eat(self) -> None:
raise Exception("A robot can not eat")
def work(self) -> None:
print("Working!")""",
"```",
sep="\n",
)
print(
"\nThis is a bad implementation of Interface Segregation Principle (ISP),",
"because the 'BadRobot' class should not implement an interface with methods",
"that it is going to never use. So, the implemented interface ('AbcBadWorker')",
"is to general for the 'BadRobot' class, but perfect for the 'BadProgrammer' class.",
sep="\n",
)
print("\nGood implementation of Interface Segregation Principle (ISP)...")
class AbcHumanWorker(metaclass=ABCMeta):
@abstractmethod
def eat(self) -> None:
pass
@abstractmethod
def work(self) -> None:
pass
class AbcNotHumanWorker(metaclass=ABCMeta):
@abstractmethod
def work(self) -> None:
pass
class GoodProgrammer(AbcHumanWorker):
def eat(self) -> None:
print("Eating!")
def work(self) -> None:
print("Working!")
class GoodRobot(AbcNotHumanWorker):
def work(self) -> None:
print("Working!")
print(
"\n```",
"""class AbcHumanWorker(metaclass=ABCMeta):
@abstractmethod
def eat(self) -> None:
pass
@abstractmethod
def work(self) -> None:
pass
class AbcNotHumanWorker(metaclass=ABCMeta):
@abstractmethod
def work(self) -> None:
pass
class GoodProgrammer(AbcHumanWorker):
def eat(self) -> None:
print("Eating!")
def work(self) -> None:
print("Working!")
class GoodRobot(AbcNotHumanWorker):
def work(self) -> None:
print("Working!")""",
"```",
sep="\n",
)
print(
"\nThis is a good implementation of Interface Segregation Principle (ISP),",
"because the 'GoodRobot' class only implements the necessary methods, without",
"any extras. Also, the 'GoodProgrammer' class utilizes an interface perfectly",
"suited to its needs, with no extra methods.",
sep="\n",
)
print(
"\n# ---------------------------------------------------------------------------------- #\n"
)
"""
Additional challenge...
"""
print("Additional challenge...")
class AbcBlackAndWhitePrinter(metaclass=ABCMeta):
@abstractmethod
def print_(self) -> None:
pass
class BlackAndWhitePrinter(AbcBlackAndWhitePrinter):
def print_(self) -> None:
print("Paper printed in black and white!")
class AbcColorPrinter(metaclass=ABCMeta):
@abstractmethod
def print_(self) -> None:
pass
class ColorPrinter(AbcColorPrinter):
def print_(self) -> None:
print("Paper printed in color!")
class AbcMultifunctionalPrinter(metaclass=ABCMeta):
@abstractmethod
def print_(self) -> None:
pass
@abstractmethod
def scan(self) -> None:
pass
@abstractmethod
def send_fax(self) -> None:
pass
class MultifunctionalPrinter(AbcMultifunctionalPrinter):
def print_(self) -> None:
print("Paper printed!")
def scan(self) -> None:
print("Scan completed!")
def send_fax(self) -> None:
print("Fax sent!")
black_and_white_printer: BlackAndWhitePrinter = BlackAndWhitePrinter()
color_printer: ColorPrinter = ColorPrinter()
multifunctional_printer: MultifunctionalPrinter = MultifunctionalPrinter()
print()
black_and_white_printer.print_()
print()
color_printer.print_()
print()
multifunctional_printer.print_()
print()
multifunctional_printer.scan()
print()
multifunctional_printer.send_fax()