|
| 1 | +""" |
| 2 | + * EJERCICIO: |
| 3 | + * Explora el patrón de diseño "singleton" y muestra cómo crearlo |
| 4 | + * con un ejemplo genérico. |
| 5 | +""" |
| 6 | + |
| 7 | +class Singleton: |
| 8 | + _instance = None |
| 9 | + |
| 10 | + def __new__(cls, *args, **kgargs): |
| 11 | + |
| 12 | + if cls._instance is None: |
| 13 | + cls._instance = super().__new__(cls) |
| 14 | + |
| 15 | + return cls._instance |
| 16 | + |
| 17 | +s1 = Singleton() |
| 18 | +print(f"First instance: {s1}") |
| 19 | +s2 = Singleton() |
| 20 | +print(f"Second instance: {s2}") |
| 21 | +print(f"s1 is s2?: {s1 is s2}") |
| 22 | + |
| 23 | + |
| 24 | +""" |
| 25 | + * DIFICULTAD EXTRA (opcional): |
| 26 | + * Utiliza el patrón de diseño "singleton" para representar una clase que |
| 27 | + * haga referencia a la sesión de usuario de una aplicación ficticia. |
| 28 | + * La sesión debe permitir asignar un usuario (id, username, nombre y email), |
| 29 | + * recuperar los datos del usuario y borrar los datos de la sesión. |
| 30 | +""" |
| 31 | + |
| 32 | +class User: |
| 33 | + _instance = None |
| 34 | + |
| 35 | + def __new__(cls, id: str, username: str, name: str, email: str): |
| 36 | + |
| 37 | + if cls._instance is None: |
| 38 | + |
| 39 | + cls._instance = super().__new__(cls) |
| 40 | + |
| 41 | + cls.id = id |
| 42 | + cls.name = name |
| 43 | + cls.username = username |
| 44 | + cls.email = email |
| 45 | + |
| 46 | + return cls._instance |
| 47 | + |
| 48 | + |
| 49 | + # These are instance attributes. If __init__ exist, get_user() method will point to these instance attributes istead the class attributes. |
| 50 | + """ |
| 51 | + def __init__(self, id: str, username: str, name: str, email: str): |
| 52 | + print("Inside init") |
| 53 | + self.id = id |
| 54 | + self.username= username |
| 55 | + self.name = name |
| 56 | + self.email = email |
| 57 | + """ |
| 58 | + |
| 59 | + |
| 60 | + def get_user(self): |
| 61 | + return { |
| 62 | + "id": self.id, |
| 63 | + "username": self.username, |
| 64 | + "name": self.name, |
| 65 | + "email": self.email, |
| 66 | + } |
| 67 | + |
| 68 | + def clean_user(self): |
| 69 | + User._instance = None |
| 70 | + |
| 71 | + |
| 72 | +################### |
| 73 | +# Testing the class |
| 74 | +################### |
| 75 | + |
| 76 | +print("") |
| 77 | +print("### Testing: Dificultad extra\n") |
| 78 | + |
| 79 | +luis = User(id= "01", username="LuisOlivares", name="Luis", email="alfonso@dev") |
| 80 | +print(f"User luis: {luis.get_user()}") |
| 81 | + |
| 82 | +print("Trying to create a new user...") |
| 83 | +luis2 = User("1", "2", "3", "4") |
| 84 | + |
| 85 | +print(f"luis is luis2?: {luis is luis2}") |
| 86 | + |
| 87 | +print(f"User luis: {luis.get_user()}") |
| 88 | +print(f"User luis2: {luis2.get_user()}") |
| 89 | + |
| 90 | +print("\nUsing clear_user method...:\n") |
| 91 | +luis.clean_user() |
| 92 | +oscar = User("2", "OscarOlivares", "Oscar", "oscar@Dev") |
| 93 | +print(f"User oscar: {oscar.get_user()}") |
0 commit comments