1
+ # ---------------------------------------------
1
2
# Ejemplo de Principio de responsabilidad única
2
- # """
3
+ # ---------------------------------------------
3
4
4
5
# El siguiente ejemplo muestra un código que viola el principio de responsabilidad única.
5
- # La clase ImageManager tiene dos responsabilidades: obtener estadísticas de una imagen y graficarla.
6
+ # La clase ImageManager tiene dos responsabilidades:
7
+ # 1.- Obtener estadísticas de una imagen
8
+ # 2.- Graficar una imagen
6
9
7
10
import matplotlib .pyplot as plt
8
11
import numpy as np
@@ -47,10 +50,13 @@ def plot(self):
47
50
plt .show ()
48
51
49
52
50
-
51
-
53
+ # ---------------------------------------------
52
54
# Dificultad extra
53
55
# Sistema de gestion para una bibioteca
56
+ #
57
+ # FORMA INCORRECTA
58
+ #
59
+ # ---------------------------------------------
54
60
55
61
logging .basicConfig (level = logging .INFO , format = "%(asctime)s - %(message)s" )
56
62
@@ -130,6 +136,8 @@ def return_book(self, title: str, name: str):
130
136
131
137
132
138
# Testing the class
139
+ print ("\n " * 3 )
140
+ print ("### Testing: Dificultad extra (FORMA INCORRECTA)\n " )
133
141
my_library = Library ()
134
142
my_library .add_user ("Alice" , 1 , "alice@dev" )
135
143
my_library .add_user ("Bob" , 2 , "bob@dev" )
@@ -152,6 +160,16 @@ def return_book(self, title: str, name: str):
152
160
my_library .print_users ()
153
161
154
162
163
+ # ---------------------------------------------
164
+ # Dificultad extra
165
+ # Sistema de gestion para una bibioteca
166
+ #
167
+ # Forma CORRECTA
168
+ #
169
+ # Se crean dos clases diferentes, BooksManager y UsersManager, para manejar los libros y a los usuarios.
170
+ # Se crea una clase que maneja la biblioteca y delega las responsabilidades a las clases BooksManeger y UsersManager.
171
+ # ---------------------------------------------
172
+
155
173
class BooksManeger :
156
174
def __init__ (self ):
157
175
self .books = []
@@ -248,9 +266,10 @@ def return_book(self, title: str, name: str):
248
266
self .books_m .return_book (title )
249
267
self .users_m .return_book (title , name )
250
268
251
- print ("\n " * 3 )
252
- print ("### Testing: Dificultad extra\n " )
269
+
253
270
# Testing the classes
271
+ print ("\n " * 3 )
272
+ print ("### Testing: Dificultad extra (FORMA CORRECTA)\n " )
254
273
255
274
my_library_v2 = Library_v2 ()
256
275
my_library_v2 .add_user ("Alice" , 1 , "alice@dev" )
0 commit comments