Last active
June 23, 2022 11:33
-
-
Save wolivera/db2bfb1ba2cce406a0e524b3c598ee92 to your computer and use it in GitHub Desktop.
GoF Singleton Pattern
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 Database { | |
constructor(url) { | |
this.url = url; | |
} | |
static getInstance() { | |
if (typeof Database.instance === 'object') { | |
return Database.instance; | |
} | |
Database.instance = new Database('http//localhost:8545'); | |
return Database.instance; | |
} | |
} | |
const instance1 = Database.getInstance(); | |
const instance2 = Database.getInstance(); | |
console.log('Is the same? ', instance1 === instance2); | |
// Is the same? true |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment