Skip to content

Commit efb27ab

Browse files
committed
Add demo to singleton
1 parent 6690399 commit efb27ab

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

singleton/src/demo.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path="singleton.ts" />
2+
namespace SingletonPattern {
3+
export namespace Demo {
4+
5+
export function show() : void {
6+
var singleton1 = SingletonPattern.Singleton.Instance;
7+
var singleton2 = SingletonPattern.Singleton.Instance;
8+
9+
if (singleton1 === singleton2) {
10+
console.log("two singletons are equivalent");
11+
} else {
12+
console.log("two singletons are not equivalent");
13+
}
14+
}
15+
}
16+
}
17+
18+
SingletonPattern.Demo.show();

singleton/src/singleton.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
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();
10-
}
11-
return this.instance;
12-
}
13-
}
14-
}
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();
10+
}
11+
return this.instance;
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)