forked from torokmark/design_patterns_in_typescript
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request torokmark#13 from rugpanov/master
Improve SingletonPattern example
- Loading branch information
Showing
2 changed files
with
10 additions
and
8 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,23 @@ | ||
namespace SingletonPattern { | ||
export class Singleton { | ||
// A variable which stores the singleton object. Intially, | ||
// A variable which stores the singleton object. Initially, | ||
// the variable acts like a placeholder | ||
private static singleton: Singleton = null; | ||
private static singleton: Singleton; | ||
|
||
// private constructor so that no instance is created | ||
private constructor() { | ||
} | ||
|
||
// This is how we create a singleton object | ||
public static Instance(): Singleton { | ||
public static getInstance(): Singleton { | ||
// check if an instance of the class is already created | ||
if (this.singleton == null) { | ||
if (!Singleton.singleton) { | ||
// If not created create an instance of the class | ||
// store the instance in the variable | ||
this.singleton = new Singleton(); | ||
Singleton.singleton = new Singleton(); | ||
} | ||
// return the singleton object | ||
return this.singleton | ||
return Singleton.singleton; | ||
} | ||
} | ||
} |