1
+ // Ejemplo genérico de Singleton
2
+ object Singleton {
3
+ init {
4
+ println (" Singleton inicializado" )
5
+ }
6
+
7
+ fun doSomething () {
8
+ println (" Singleton está haciendo algo" )
9
+ }
10
+ }
11
+
12
+ // Ejemplo de la dificultad extra: Sesión de usuario
13
+ object UserSession {
14
+ private var userId: Int? = null
15
+ private var username: String? = null
16
+ private var name: String? = null
17
+ private var email: String? = null
18
+
19
+ fun setUser (id : Int , username : String , name : String , email : String ) {
20
+ this .userId = id
21
+ this .username = username
22
+ this .name = name
23
+ this .email = email
24
+ println (" Usuario asignado a la sesión" )
25
+ }
26
+
27
+ fun getUserData (): Map <String , Any ?> {
28
+ return mapOf (
29
+ " id" to userId,
30
+ " username" to username,
31
+ " name" to name,
32
+ " email" to email
33
+ )
34
+ }
35
+
36
+ fun clearSession () {
37
+ userId = null
38
+ username = null
39
+ name = null
40
+ email = null
41
+ println (" Sesión borrada" )
42
+ }
43
+ }
44
+
45
+ fun main () {
46
+ // Uso del Singleton genérico
47
+ println (" Usando el Singleton genérico:" )
48
+ Singleton .doSomething()
49
+ Singleton .doSomething()
50
+
51
+ println (" \n Usando el UserSession:" )
52
+ // Uso del UserSession
53
+ UserSession .setUser(
1 ,
" eulogioep" ,
" Eulogio EP" ,
" [email protected] " )
54
+ println (" Datos del usuario: ${UserSession .getUserData()} " )
55
+ UserSession .clearSession()
56
+ println (" Datos después de borrar: ${UserSession .getUserData()} " )
57
+ }
0 commit comments