Last active
August 24, 2023 16:48
-
-
Save dapize/c70d387e720a85e3e0ba6a5d795c1a6c to your computer and use it in GitHub Desktop.
Singleton Pattern in Typescript
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
class Office { | |
name: string; | |
employees: number; | |
private static instance: Office; | |
constructor(name: string, employees: number) { | |
this.name = name; | |
this.employees = employees; | |
if (typeof Office.instance === "object") { | |
return Office.instance; | |
} | |
Office.instance = this; | |
return this; | |
} | |
} | |
const office1 = new Office("Principal", 30); | |
console.log(office1); | |
const office2 = new Office("Diagonal", 50); | |
console.log(office2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment