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.
- Loading branch information
1 parent
753bf90
commit ea27d69
Showing
1 changed file
with
90 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,90 @@ | ||
import Foundation | ||
|
||
|
||
|
||
class Singleton { | ||
// Propiedad estática que contiene la única instancia de la clase | ||
static let shared = Singleton() | ||
|
||
// Inicializador privado para prevenir la creación de otras instancias | ||
private init() {} | ||
|
||
// Métodos y propiedades de la clase | ||
func sayHi(to name: String) { | ||
print("Hi, \(name)") | ||
} | ||
} | ||
|
||
// Acceso global desde cualquier parte del código | ||
let instance1 = Singleton.shared | ||
instance1.sayHi(to: "Charly") | ||
|
||
let instance2 = Singleton.shared | ||
instance2.sayHi(to: "Marta") | ||
|
||
// Verificar que ambas referencias apuntan a la misma instancia | ||
print(instance1 === instance2) // true | ||
|
||
|
||
|
||
|
||
// DIFICULTAD EXTRA | ||
print("\nDificultad Extra.\n") | ||
|
||
|
||
class UserSession { | ||
static let shared = UserSession() | ||
|
||
private init() {} | ||
|
||
var id: String = "" | ||
var userName: String = "" | ||
var name: String = "" | ||
var email: String = "" | ||
|
||
func assigntID(_ id: String) { | ||
self.id = id | ||
} | ||
|
||
func assigntUserName(_ userName: String) { | ||
self.userName = userName | ||
} | ||
|
||
func assigntName(_ name: String) { | ||
self.name = name | ||
} | ||
|
||
func assignEmail(_ email: String) { | ||
self.email = email | ||
} | ||
|
||
func showSessionInfo() { | ||
print("id: \(id)") | ||
print("User Name: \(userName)") | ||
print("Name: \(name)") | ||
print("email: \(email)") | ||
} | ||
|
||
func removeSession() { | ||
id = "" | ||
userName = "" | ||
name = "" | ||
email = "" | ||
print("\nSession Removed") | ||
} | ||
} | ||
|
||
|
||
let userSession = UserSession.shared | ||
|
||
userSession.assigntID(UUID().uuidString) | ||
userSession.assigntUserName("Teophilus") | ||
userSession.assigntName("Pablo") | ||
userSession.assignEmail("[email protected]") | ||
|
||
userSession.showSessionInfo() | ||
userSession.removeSession() | ||
|
||
|
||
|
||
|