Skip to content

Commit

Permalink
Merge pull request #4237 from LuisOlivaresJ/main
Browse files Browse the repository at this point in the history
#23 Python
  • Loading branch information
kontroldev authored Jun 11, 2024
2 parents 4e24922 + 0c58a67 commit b7c55c2
Showing 1 changed file with 93 additions and 0 deletions.
93 changes: 93 additions & 0 deletions Roadmap/23 - SINGLETON/python/LuisOlivaresJ.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
"""
* EJERCICIO:
* Explora el patrón de diseño "singleton" y muestra cómo crearlo
* con un ejemplo genérico.
"""

class Singleton:
_instance = None

def __new__(cls, *args, **kgargs):

if cls._instance is None:
cls._instance = super().__new__(cls)

return cls._instance

s1 = Singleton()
print(f"First instance: {s1}")
s2 = Singleton()
print(f"Second instance: {s2}")
print(f"s1 is s2?: {s1 is s2}")


"""
* DIFICULTAD EXTRA (opcional):
* Utiliza el patrón de diseño "singleton" para representar una clase que
* haga referencia a la sesión de usuario de una aplicación ficticia.
* La sesión debe permitir asignar un usuario (id, username, nombre y email),
* recuperar los datos del usuario y borrar los datos de la sesión.
"""

class User:
_instance = None

def __new__(cls, id: str, username: str, name: str, email: str):

if cls._instance is None:

cls._instance = super().__new__(cls)

cls.id = id
cls.name = name
cls.username = username
cls.email = email

return cls._instance


# These are instance attributes. If __init__ exist, get_user() method will point to these instance attributes istead the class attributes.
"""
def __init__(self, id: str, username: str, name: str, email: str):
print("Inside init")
self.id = id
self.username= username
self.name = name
self.email = email
"""


def get_user(self):
return {
"id": self.id,
"username": self.username,
"name": self.name,
"email": self.email,
}

def clean_user(self):
User._instance = None


###################
# Testing the class
###################

print("")
print("### Testing: Dificultad extra\n")

luis = User(id= "01", username="LuisOlivares", name="Luis", email="alfonso@dev")
print(f"User luis: {luis.get_user()}")

print("Trying to create a new user...")
luis2 = User("1", "2", "3", "4")

print(f"luis is luis2?: {luis is luis2}")

print(f"User luis: {luis.get_user()}")
print(f"User luis2: {luis2.get_user()}")

print("\nUsing clear_user method...:\n")
luis.clean_user()
oscar = User("2", "OscarOlivares", "Oscar", "oscar@Dev")
print(f"User oscar: {oscar.get_user()}")

0 comments on commit b7c55c2

Please sign in to comment.