Last active
December 19, 2021 08:12
-
-
Save msankhala/ea7d514e356b2352c9daf979d055cdb3 to your computer and use it in GitHub Desktop.
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
// 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