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.
- Loading branch information
Showing
2 changed files
with
32 additions
and
14 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 |
---|---|---|
@@ -0,0 +1,18 @@ | ||
/// <reference path="singleton.ts" /> | ||
namespace SingletonPattern { | ||
export namespace Demo { | ||
|
||
export function show() : void { | ||
var singleton1 = SingletonPattern.Singleton.Instance; | ||
var singleton2 = SingletonPattern.Singleton.Instance; | ||
|
||
if (singleton1 === singleton2) { | ||
console.log("two singletons are equivalent"); | ||
} else { | ||
console.log("two singletons are not equivalent"); | ||
} | ||
} | ||
} | ||
} | ||
|
||
SingletonPattern.Demo.show(); |
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,14 @@ | ||
namespace SingletonPattern { | ||
export class Singleton { | ||
private static instance: Singleton; | ||
|
||
constructor() {} | ||
|
||
static get Instance() { | ||
if (this.instance === null || this.instance === undefined) { | ||
this.instance = new Singleton(); | ||
} | ||
return this.instance; | ||
} | ||
} | ||
} | ||
namespace SingletonPattern { | ||
export class Singleton { | ||
private static instance: Singleton; | ||
|
||
constructor() {} | ||
|
||
static get Instance() { | ||
if (this.instance === null || this.instance === undefined) { | ||
this.instance = new Singleton(); | ||
} | ||
return this.instance; | ||
} | ||
} | ||
} |