Skip to content

Instantly share code, notes, and snippets.

@wolivera
Last active June 23, 2022 11:33
Show Gist options
  • Save wolivera/db2bfb1ba2cce406a0e524b3c598ee92 to your computer and use it in GitHub Desktop.
Save wolivera/db2bfb1ba2cce406a0e524b3c598ee92 to your computer and use it in GitHub Desktop.
GoF Singleton Pattern
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