Skip to content

Instantly share code, notes, and snippets.

@msankhala
Last active December 19, 2021 08:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save msankhala/ea7d514e356b2352c9daf979d055cdb3 to your computer and use it in GitHub Desktop.
Save msankhala/ea7d514e356b2352c9daf979d055cdb3 to your computer and use it in GitHub Desktop.
// The 4 Creational Design Patterns In Node.js
// https://daily.dev/blog/the-4-creational-design-patterns-in-node-js-you-should-know
// The Singletone Pattern
class DatabaseConnection {
constructor() {
this.databaseConnection = 'dummytext';
}
getNewDBConnection() {
return this.databaseConnection;
}
}
class Singleton {
constructor() {
throw new Error('Use the getInstance() method on the Singleton object!');
}
getInstance() {
if (!Singleton.instance) {
Singleton.instance = new DatabaseConnection();
}
return Singleton.instance;
}
}
module.exports = Singleton;
// or
class DatabaseConnection {
constructor() {
this.databaseConnection = 'dummytext';
}
getNewDBConnection() {
return this.databaseConnection;
}
}
module.exports = new DatabaseConnection();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment