Skip to content

Commit 411255f

Browse files
authored
Merge pull request mouredev#4251 from Kenysdev/23.cs
#23 - c#
2 parents 80e5a38 + 57f871e commit 411255f

File tree

1 file changed

+118
-0
lines changed

1 file changed

+118
-0
lines changed

Roadmap/23 - SINGLETON/c#/kenysdev.cs

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
#pragma warning disable CA1050
2+
/*
3+
╔══════════════════════════════════════╗
4+
║ Autor: Kenys Alvarado ║
5+
║ GitHub: https://github.com/Kenysdev ║
6+
║ 2024 - C# ║
7+
╚══════════════════════════════════════╝
8+
------------------------------------------
9+
* PATRONES DE DISEÑO: SINGLETON
10+
------------------------------------------
11+
12+
* EJERCICIO #1:
13+
* Explora el patrón de diseño "singleton" y muestra cómo crearlo
14+
* con un ejemplo genérico.
15+
*/
16+
public class Singleton {
17+
private static Singleton _instance;
18+
19+
// Constructor privado para evitar la instanciación directa.
20+
private Singleton() { }
21+
22+
// Método estático que devuelve la única instancia de la clase.
23+
public static Singleton Instance {
24+
get {
25+
// Si la instancia aún no ha sido creada, la crea.
26+
_instance ??= new Singleton();
27+
return _instance;
28+
}
29+
}
30+
}
31+
32+
/*
33+
__________________________________
34+
* EJERCICIO #2:
35+
* Utiliza el patrón de diseño "singleton" para representar una clase que
36+
* haga referencia a la sesión de usuario de una aplicación ficticia.
37+
* La sesión debe permitir asignar un usuario (id, username, nombre y email),
38+
* recuperar los datos del usuario y borrar los datos de la sesión.
39+
*/
40+
41+
public class UserSession {
42+
private static UserSession _instance;
43+
private int _userId;
44+
private string _userName;
45+
private string _name;
46+
private string _email;
47+
private UserSession() { }
48+
49+
public static UserSession Instance {
50+
get {
51+
// Si la instancia aún no ha sido creada, la crea.
52+
_instance ??= new UserSession();
53+
return _instance;
54+
}
55+
}
56+
57+
// Método para establecer los detalles del usuario.
58+
public void SetUser(int userId, string userName, string name, string email) {
59+
_userId = userId;
60+
_userName = userName;
61+
_name = name;
62+
_email = email;
63+
}
64+
65+
public Dictionary<string, object> GetUser() {
66+
Dictionary<string, object> userDetails = new() {
67+
{ "id", _userId },
68+
{ "username", _userName },
69+
{ "name", _name },
70+
{ "email", _email }
71+
};
72+
return userDetails;
73+
}
74+
75+
public void Logout() {
76+
_userId = 0;
77+
_userName = null;
78+
_name = null;
79+
_email = null;
80+
}
81+
}
82+
83+
// __________________________________
84+
class Program {
85+
static void Main() {
86+
Console.WriteLine("EJERCICIO #1");
87+
88+
Singleton singleton1 = Singleton.Instance;
89+
90+
// singleton2 accede a la misma instancia que singleton1.
91+
Singleton singleton2 = Singleton.Instance;
92+
93+
// Comprobación de igualdad de referencias.
94+
Console.WriteLine(singleton1 == singleton2);
95+
96+
// __________________________________
97+
Console.WriteLine("EJERCICIO #2");
98+
99+
var login_user1 = UserSession.Instance;
100+
login_user1.SetUser(1, "Zoe_1", "Zoe", "[email protected]");
101+
Dictionary<string, object> userDetails1 = login_user1.GetUser();
102+
foreach (var kvp in userDetails1) {
103+
Console.WriteLine($"{kvp.Key}: {kvp.Value}");
104+
};
105+
106+
login_user1.Logout();
107+
108+
var login_user2 = UserSession.Instance;
109+
login_user2.SetUser(2, "Ben_1", "Ben", "[email protected]");
110+
Dictionary<string, object> userDetails2 = login_user2.GetUser();
111+
foreach (var kvp in userDetails2) {
112+
Console.WriteLine($"{kvp.Key}: {kvp.Value}");
113+
}
114+
115+
login_user2.Logout();
116+
117+
}
118+
}

0 commit comments

Comments
 (0)