Skip to content

Commit bfef359

Browse files
committed
Add demo to prototype
1 parent c00b2ad commit bfef359

File tree

2 files changed

+69
-63
lines changed

2 files changed

+69
-63
lines changed

prototype/demo.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// <reference path="prototype.ts" />
2+
3+
namespace PrototypePattern {
4+
export namespace Demo {
5+
export function show() : void {
6+
var builder : PrototypePattern.Builder = new PrototypePattern.Builder();
7+
var i = 0;
8+
for (i = 1; i <= 3; i += 1) {
9+
console.log(builder.createOne("c" + i).toString());
10+
}
11+
12+
}
13+
}
14+
}

prototype/prototype.ts

Lines changed: 55 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,55 @@
1-
namespace PrototypePattern {
2-
export interface Prototype {
3-
clone(): Prototype;
4-
toString(): string;
5-
}
6-
7-
export class Concrete1 implements Prototype {
8-
9-
clone() : Prototype {
10-
return new Concrete1();
11-
}
12-
13-
toString(): string {
14-
return "This is Concrete1";
15-
}
16-
}
17-
18-
export class Concrete2 implements Prototype {
19-
20-
clone() : Prototype {
21-
return new Concrete2();
22-
}
23-
24-
toString(): string {
25-
return "This is Concrete2";
26-
}
27-
}
28-
29-
export class Concrete3 implements Prototype {
30-
31-
clone() : Prototype {
32-
return new Concrete3();
33-
}
34-
35-
toString(): string {
36-
return "This is Concrete3";
37-
}
38-
}
39-
40-
41-
export class Builder {
42-
private prototypeMap: { [s: string]: Prototype; } = {};
43-
44-
constructor() {
45-
this.prototypeMap['c1'] = new Concrete1();
46-
this.prototypeMap['c2'] = new Concrete2();
47-
this.prototypeMap['c3'] = new Concrete3();
48-
}
49-
50-
createOne(s: string): Prototype {
51-
console.log(s);
52-
return this.prototypeMap[s].clone();
53-
}
54-
}
55-
}
56-
57-
(function main() {
58-
var builder : PrototypePattern.Builder = new PrototypePattern.Builder();
59-
var i = 0;
60-
for (i = 1; i <= 3; i += 1) {
61-
console.log(builder.createOne("c" + i).toString());
62-
}
63-
}());
1+
namespace PrototypePattern {
2+
export interface Prototype {
3+
clone(): Prototype;
4+
toString(): string;
5+
}
6+
7+
export class Concrete1 implements Prototype {
8+
9+
clone() : Prototype {
10+
return new Concrete1();
11+
}
12+
13+
toString(): string {
14+
return "This is Concrete1";
15+
}
16+
}
17+
18+
export class Concrete2 implements Prototype {
19+
20+
clone() : Prototype {
21+
return new Concrete2();
22+
}
23+
24+
toString(): string {
25+
return "This is Concrete2";
26+
}
27+
}
28+
29+
export class Concrete3 implements Prototype {
30+
31+
clone() : Prototype {
32+
return new Concrete3();
33+
}
34+
35+
toString(): string {
36+
return "This is Concrete3";
37+
}
38+
}
39+
40+
41+
export class Builder {
42+
private prototypeMap: { [s: string]: Prototype; } = {};
43+
44+
constructor() {
45+
this.prototypeMap['c1'] = new Concrete1();
46+
this.prototypeMap['c2'] = new Concrete2();
47+
this.prototypeMap['c3'] = new Concrete3();
48+
}
49+
50+
createOne(s: string): Prototype {
51+
console.log(s);
52+
return this.prototypeMap[s].clone();
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)