|
| 1 | +/* |
| 2 | + Singleton pattern... |
| 3 | +*/ |
| 4 | + |
| 5 | +console.log('Singleton pattern...') |
| 6 | + |
| 7 | +interface ICounter { |
| 8 | + getCount(): Counter['count'] |
| 9 | + decrement(): this |
| 10 | + increment(): this |
| 11 | +} |
| 12 | + |
| 13 | +class Counter implements ICounter { |
| 14 | + private static instance: Counter |
| 15 | + |
| 16 | + private count: number |
| 17 | + |
| 18 | + private constructor() { |
| 19 | + this.count = 0 |
| 20 | + } |
| 21 | + |
| 22 | + public static getInstance(): Counter { |
| 23 | + if (!Counter.instance) { |
| 24 | + Counter.instance = new Counter() |
| 25 | + } |
| 26 | + |
| 27 | + return Counter.instance |
| 28 | + } |
| 29 | + |
| 30 | + public getCount(): number { |
| 31 | + return this.count |
| 32 | + } |
| 33 | + |
| 34 | + public decrement(): this { |
| 35 | + this.count-- |
| 36 | + return this |
| 37 | + } |
| 38 | + |
| 39 | + public increment(): this { |
| 40 | + this.count++ |
| 41 | + return this |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +const counter01: Counter = Counter.getInstance() |
| 46 | +const counter02: Counter = Counter.getInstance() |
| 47 | + |
| 48 | +console.log( |
| 49 | + '\nAre `counter01` and `counter02` the same instance of `Counter` class?', |
| 50 | + counter01 === counter02 |
| 51 | +) |
| 52 | + |
| 53 | +console.log('\nMethod call of `counter01` instance...') |
| 54 | + |
| 55 | +counter01.increment().increment().increment() |
| 56 | +console.log('\ncounter01.increment().increment().increment()') |
| 57 | + |
| 58 | +console.log( |
| 59 | + '\nCount attribute of `counter01` instance -->', |
| 60 | + counter01.getCount() |
| 61 | +) |
| 62 | + |
| 63 | +console.log('\nMethod call of `counter02` instance...') |
| 64 | + |
| 65 | +counter02.decrement() |
| 66 | +console.log('\ncounter02.decrement()') |
| 67 | + |
| 68 | +console.log( |
| 69 | + '\nCount attribute of `counter02` instance -->', |
| 70 | + counter02.getCount() |
| 71 | +) |
| 72 | + |
| 73 | +console.log( |
| 74 | + '\n# ---------------------------------------------------------------------------------- #\n' |
| 75 | +) |
| 76 | + |
| 77 | +/* |
| 78 | + Additional challenge... |
| 79 | +*/ |
| 80 | + |
| 81 | +console.log('Additional challenge...') |
| 82 | + |
| 83 | +interface IUserSession { |
| 84 | + getEmail(): UserSession['email'] |
| 85 | + getId(): UserSession['id'] |
| 86 | + getName(): UserSession['name'] |
| 87 | + getUserName(): UserSession['userName'] |
| 88 | + |
| 89 | + setEmail(newEmail: UserSession['email']): this |
| 90 | + setId(newId: UserSession['id']): this |
| 91 | + setName(newName: UserSession['name']): this |
| 92 | + setUserName(newUserName: UserSession['userName']): this |
| 93 | + |
| 94 | + deleteData(): this |
| 95 | +} |
| 96 | + |
| 97 | +interface AUserSession { |
| 98 | + email: string |
| 99 | + id: string |
| 100 | + name: string |
| 101 | + userName: string |
| 102 | +} |
| 103 | + |
| 104 | +class UserSession implements IUserSession { |
| 105 | + private static instance: UserSession |
| 106 | + |
| 107 | + private email: AUserSession['email'] |
| 108 | + private id: AUserSession['email'] |
| 109 | + private name: AUserSession['email'] |
| 110 | + private userName: AUserSession['email'] |
| 111 | + |
| 112 | + private constructor({email, id, name, userName}: AUserSession) { |
| 113 | + this.email = email |
| 114 | + this.id = id |
| 115 | + this.name = name |
| 116 | + this.userName = userName |
| 117 | + } |
| 118 | + |
| 119 | + public static getInstance( |
| 120 | + { |
| 121 | + email = '', |
| 122 | + id = '', |
| 123 | + name = '', |
| 124 | + userName = '', |
| 125 | + }: Partial<AUserSession> = { |
| 126 | + email: '', |
| 127 | + id: '', |
| 128 | + name: '', |
| 129 | + userName: '', |
| 130 | + } |
| 131 | + ): UserSession { |
| 132 | + if (!UserSession.instance) { |
| 133 | + UserSession.instance = new UserSession({ |
| 134 | + email, |
| 135 | + id, |
| 136 | + name, |
| 137 | + userName, |
| 138 | + }) |
| 139 | + } |
| 140 | + |
| 141 | + return UserSession.instance |
| 142 | + } |
| 143 | + |
| 144 | + public getEmail(): AUserSession['email'] { |
| 145 | + return this.email |
| 146 | + } |
| 147 | + |
| 148 | + public getId(): AUserSession['id'] { |
| 149 | + return this.id |
| 150 | + } |
| 151 | + |
| 152 | + public getName(): AUserSession['name'] { |
| 153 | + return this.name |
| 154 | + } |
| 155 | + |
| 156 | + public getUserName(): AUserSession['userName'] { |
| 157 | + return this.userName |
| 158 | + } |
| 159 | + |
| 160 | + public setEmail(newEmail: AUserSession['email']): this { |
| 161 | + this.email = newEmail |
| 162 | + return this |
| 163 | + } |
| 164 | + |
| 165 | + public setId(newId: AUserSession['id']): this { |
| 166 | + this.id = newId |
| 167 | + return this |
| 168 | + } |
| 169 | + |
| 170 | + public setName(newName: AUserSession['name']): this { |
| 171 | + this.name = newName |
| 172 | + return this |
| 173 | + } |
| 174 | + |
| 175 | + public setUserName(newUserName: AUserSession['userName']): this { |
| 176 | + this.userName = newUserName |
| 177 | + return this |
| 178 | + } |
| 179 | + |
| 180 | + public deleteData(): this { |
| 181 | + this.email = '' |
| 182 | + this.id = '' |
| 183 | + this.name = '' |
| 184 | + this.userName = '' |
| 185 | + |
| 186 | + return this |
| 187 | + } |
| 188 | +} |
| 189 | + |
| 190 | +const userSession: UserSession = UserSession.getInstance({ |
| 191 | + |
| 192 | + id: '155-AV25-12Z', |
| 193 | + name: 'Juan', |
| 194 | + userName: 'Juanceto01', |
| 195 | +}) |
| 196 | + |
| 197 | +console.log( |
| 198 | + '\nUser:', |
| 199 | + userSession.getId(), |
| 200 | + userSession.getName(), |
| 201 | + userSession.getUserName(), |
| 202 | + userSession.getEmail() |
| 203 | +) |
| 204 | + |
| 205 | +userSession.deleteData() |
| 206 | +console.log('\nUser data deleted!') |
| 207 | + |
| 208 | +console.log( |
| 209 | + '\nUser:', |
| 210 | + userSession.getId(), |
| 211 | + userSession.getName(), |
| 212 | + userSession.getUserName(), |
| 213 | + userSession.getEmail() |
| 214 | +) |
0 commit comments