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#8 from Pluralsights/master
new approach for singleton pattern
- Loading branch information
Showing
1 changed file
with
17 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,21 @@ | ||
namespace SingletonPattern { | ||
export class Singleton { | ||
private static instance: Singleton; | ||
|
||
constructor() {} | ||
|
||
static get Instance() { | ||
if (this.instance === null || this.instance === undefined) { | ||
this.instance = new Singleton(); | ||
namespace Singleton { | ||
class Singleton { | ||
// A variable which stores the singleton object. Intially, | ||
// the variable acts like a placeholder | ||
private static __singleton: Singleton = null; | ||
// private constructor so that no instance is created | ||
private constructor() { | ||
} | ||
// This is how we create a singleton object | ||
public static getInstance(): Singleton { | ||
// check if an instance of the class is already created | ||
if (this.__singleton == null) { | ||
// If not created create an instance of the class | ||
// store the instance in the variable | ||
this.__singleton = new Singleton(); | ||
} | ||
return this.instance; | ||
// return the singleton object | ||
return this.__singleton | ||
} | ||
} | ||
} |