Skip to content

Instantly share code, notes, and snippets.

@dapize
Last active August 24, 2023 16:48
Show Gist options
  • Save dapize/c70d387e720a85e3e0ba6a5d795c1a6c to your computer and use it in GitHub Desktop.
Save dapize/c70d387e720a85e3e0ba6a5d795c1a6c to your computer and use it in GitHub Desktop.
Singleton Pattern in Typescript
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