forked from mouredev/roadmap-retos-programacion
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mouredev#4342 from Chrisdev00/Chrisdev00-branch
mouredev#23 - python y javascript
- Loading branch information
Showing
2 changed files
with
188 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/* | ||
* EJERCICIO: | ||
* Explora el patrón de diseño "singleton" y muestra cómo crearlo | ||
* con un ejemplo genérico. | ||
* | ||
* 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 Singleton{ | ||
constructor(){ | ||
if(Singleton.instance){ | ||
return Singleton.instance; | ||
} | ||
Singleton.instance = this; | ||
|
||
this.data = "Some data"; | ||
} | ||
|
||
getData(){ | ||
return this.data; | ||
} | ||
|
||
setData(data){ | ||
this.data = data; | ||
} | ||
} | ||
|
||
const instance1 = new Singleton(); | ||
const instance2 = new Singleton(); | ||
|
||
console.log(instance1 === instance2); | ||
|
||
instance1.setData("New Data"); | ||
|
||
console.log(instance2.getData()); | ||
|
||
|
||
// Otra forma de implementar Singleton usando IIFE | ||
|
||
const Singleton = (function() { | ||
let instance; | ||
|
||
function createInstance() { | ||
const object = new Object("I am the instance"); | ||
return object; | ||
} | ||
|
||
return { | ||
getInstance: function() { | ||
if (!instance) { | ||
instance = createInstance(); | ||
} | ||
return instance; | ||
} | ||
}; | ||
})(); | ||
|
||
const instance3 = Singleton.getInstance(); | ||
const instance4 = Singleton.getInstance(); | ||
|
||
console.log(instance3 === instance4); | ||
|
||
|
||
|
||
///////////////// ---------------------------------- EXTRA ------------------------------ /////////////////////// | ||
|
||
class UserSession{ | ||
constructor(){ | ||
if(UserSession.instance){ | ||
return UserSession.instance; | ||
} | ||
UserSession.instance = this; | ||
|
||
this.data = null; | ||
} | ||
|
||
getData(){ | ||
return this.data; | ||
} | ||
|
||
setData(userId, username, nombre, email){ | ||
this.data = { | ||
id: userId, | ||
username: username, | ||
nombre: nombre, | ||
email: email | ||
}; | ||
} | ||
|
||
clearSession(){ | ||
this.data = null; | ||
} | ||
} | ||
|
||
const session1 = new UserSession(); | ||
const session2 = new UserSession(); | ||
|
||
console.log(session1 === session2); | ||
|
||
session1.setData(1, "jhon00", "Jhon", "[email protected]"); | ||
|
||
console.log(session2.getData()); | ||
|
||
session2.clearSession(); | ||
|
||
console.log(session1.getData()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
""" | ||
* EJERCICIO: | ||
* Explora el patrón de diseño "singleton" y muestra cómo crearlo | ||
* con un ejemplo genérico. | ||
* | ||
* 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 Singleton: | ||
__instance = None | ||
|
||
def __new__(cls): | ||
if cls.__instance is None: | ||
cls.__instance = super(Singleton, cls).__new__(cls) | ||
|
||
cls.__instance.data = "Singleton Data" | ||
return cls.__instance | ||
|
||
|
||
if __name__ == "__main__": | ||
s1 = Singleton() | ||
s2 = Singleton() | ||
|
||
print(s1 is s2) | ||
|
||
print(s1.data) # Salida: Singleton Data | ||
print(s2.data) # Salida: Singleton Data | ||
|
||
s1.data = "New Singleton Data" | ||
print(s2.data) # Salida: New Singleton Data | ||
|
||
|
||
############### ------------------------------ EXTRA --------------------------------- ####################### | ||
|
||
|
||
class UsserSession: | ||
__instance = None | ||
|
||
def __new__(cls): | ||
if cls.__instance is None: | ||
cls.__instance = super(UsserSession, cls).__new__(cls) | ||
cls.__instance.user_data = {} | ||
return cls.__instance | ||
|
||
def set_user(self, id, username, name, email): | ||
self.user_data["id"] = id | ||
self.user_data["user_name"] = username | ||
self.user_data["name"] = name | ||
self.user_data["email"] = email | ||
|
||
def get_user(self): | ||
return self.user_data | ||
|
||
def cler_session(self): | ||
self.user_data = {} | ||
|
||
if __name__== "__main__": | ||
session1 = UsserSession() | ||
|
||
session1.set_user(id=1, username="jhon00", name="Jhon", email="[email protected]") | ||
|
||
user_info = session1.get_user() | ||
print(user_info) | ||
|
||
session2 = UsserSession() | ||
print(session1 is session2) | ||
|
||
user_info = session2.get_user() | ||
print(user_info) | ||
|
||
session2.cler_session() | ||
|
||
user_info = session1.get_user() | ||
print(user_info) |