File tree Expand file tree Collapse file tree 1 file changed +17
-10
lines changed Expand file tree Collapse file tree 1 file changed +17
-10
lines changed Original file line number Diff line number Diff line change 1- namespace SingletonPattern {
2- export class Singleton {
3- private static instance : Singleton ;
4-
5- constructor ( ) { }
6-
7- static get Instance ( ) {
8- if ( this . instance === null || this . instance === undefined ) {
9- this . instance = new Singleton ( ) ;
1+ namespace Singleton {
2+ class Singleton {
3+ // A variable which stores the singleton object. Intially,
4+ // the variable acts like a placeholder
5+ private static __singleton : Singleton = null ;
6+ // private constructor so that no instance is created
7+ private constructor ( ) {
8+ }
9+ // This is how we create a singleton object
10+ public static getInstance ( ) : Singleton {
11+ // check if an instance of the class is already created
12+ if ( this . __singleton == null ) {
13+ // If not created create an instance of the class
14+ // store the instance in the variable
15+ this . __singleton = new Singleton ( ) ;
1016 }
11- return this . instance ;
17+ // return the singleton object
18+ return this . __singleton
1219 }
1320 }
1421}
You can’t perform that action at this time.
0 commit comments