Created
November 27, 2020 01:57
-
-
Save misterpoloy/e02a8ecf3a652fd44568568b042644d9 to your computer and use it in GitHub Desktop.
Singleton pattern
This file contains hidden or 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
| let instance = null; | |
| class Printer { | |
| constructor(pages) { | |
| this.display= function(){ | |
| console.log(`You are connected to the printer. You want to print ${pages} pages.`) | |
| } | |
| } | |
| static getInstance(numOfpages){ | |
| if(!instance){ | |
| instance = new Printer(numOfpages); | |
| } | |
| return instance; | |
| } | |
| } | |
| var obj1 = Printer.getInstance(2) | |
| console.log(obj1) | |
| obj1.display() | |
| var obj2 = Printer.getInstance(3) | |
| console.log(obj2) | |
| obj2.display() | |
| console.log(obj2 == obj1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment