|
| 1 | +// Dominio |
| 2 | +interface IUser { |
| 3 | + id: number; |
| 4 | + first_name: string; |
| 5 | + last_name: string; |
| 6 | + email: string; |
| 7 | + birthday: Date; |
| 8 | +} |
| 9 | + |
| 10 | +// Entidad inmutable |
| 11 | +class User implements IUser { |
| 12 | + readonly id: number; |
| 13 | + readonly first_name: string; |
| 14 | + readonly last_name: string; |
| 15 | + readonly email: string; |
| 16 | + readonly birthday: Date; |
| 17 | + |
| 18 | + constructor(props: IUser) { |
| 19 | + this.id = props.id; |
| 20 | + this.first_name = props.first_name; |
| 21 | + this.last_name = props.last_name; |
| 22 | + this.email = props.email; |
| 23 | + this.birthday = new Date(props.birthday); // defensivo |
| 24 | + Object.freeze(this); // inmutabilidad superficial |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +// Singleton: único gestor de sesiones en el proceso |
| 29 | +class SessionManager { |
| 30 | + private static instance?: SessionManager; |
| 31 | + private store = new Map<number, User>(); |
| 32 | + |
| 33 | + private constructor() {} |
| 34 | + |
| 35 | + static getInstance(): SessionManager { |
| 36 | + if (!this.instance) this.instance = new SessionManager(); |
| 37 | + return this.instance; |
| 38 | + } |
| 39 | + |
| 40 | + // Crea o retorna sesión existente para el user.id |
| 41 | + login(userData: IUser): User { |
| 42 | + const existing = this.store.get(userData.id); |
| 43 | + if (existing) return existing; |
| 44 | + |
| 45 | + const user = new User(userData); |
| 46 | + this.store.set(user.id, user); |
| 47 | + return user; |
| 48 | + } |
| 49 | + |
| 50 | + getById(userId: number): User | undefined { |
| 51 | + return this.store.get(userId); |
| 52 | + } |
| 53 | + |
| 54 | + // true si removió, false si no existía |
| 55 | + logout(userId: number): boolean { |
| 56 | + return this.store.delete(userId); |
| 57 | + } |
| 58 | + |
| 59 | + // lectura segura |
| 60 | + list(): ReadonlyArray<User> { |
| 61 | + return [...this.store.values()]; |
| 62 | + } |
| 63 | + |
| 64 | + clear(): void { |
| 65 | + this.store.clear(); |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// ================== DEMO ================== |
| 70 | +const session = SessionManager.getInstance(); |
| 71 | + |
| 72 | +const user1: IUser = { |
| 73 | + id: 1, |
| 74 | + first_name: "Maria", |
| 75 | + last_name: "Luna", |
| 76 | + birthday: new Date("2020-01-02"), |
| 77 | + |
| 78 | +}; |
| 79 | + |
| 80 | +const user2: IUser = { |
| 81 | + id: 2, |
| 82 | + first_name: "Carmen", |
| 83 | + last_name: "Luna", |
| 84 | + birthday: new Date("2022-04-10"), |
| 85 | + |
| 86 | +}; |
| 87 | + |
| 88 | +const s1 = session.login(user1); |
| 89 | +const s2 = session.login(user2); |
| 90 | +const s3 = session.login(user1); |
| 91 | + |
| 92 | +console.log("s1 === s2 ?", s1 === s2); // false |
| 93 | +console.log("s1 === s3 ?", s1 === s3); // true |
| 94 | +console.log("list before logout:", session.list()); |
| 95 | + |
| 96 | +session.logout(2); |
| 97 | +console.log("list after logout:", session.list()); |
| 98 | + |
| 99 | +// script for run |
| 100 | +// npx ts-node "Roadmap/23 - SINGLETON/typescript/marialunatito.ts" |
0 commit comments