Skip to content

Commit c00b2ad

Browse files
committed
Add demo to builder
1 parent edeb4ac commit c00b2ad

File tree

2 files changed

+84
-78
lines changed

2 files changed

+84
-78
lines changed

builder/builder.ts

Lines changed: 69 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,69 @@
1-
namespace BuilderPattern {
2-
export class UserBuilder {
3-
private name: string;
4-
private age: number;
5-
private phone: string;
6-
private address: string;
7-
8-
constructor(name: string) {
9-
this.name = name;
10-
}
11-
12-
get Name() {
13-
return this.name;
14-
}
15-
setAge(value: number): UserBuilder {
16-
this.age = value;
17-
return this;
18-
}
19-
get Age() {
20-
return this.age;
21-
}
22-
setPhone(value: string): UserBuilder {
23-
this.phone = value;
24-
return this;
25-
}
26-
get Phone() {
27-
return this.phone;
28-
}
29-
setAddress(value: string): UserBuilder {
30-
this.address = value;
31-
return this;
32-
}
33-
get Address() {
34-
return this.address;
35-
}
36-
37-
build(): User {
38-
return new User(this);
39-
}
40-
}
41-
42-
export class User {
43-
private name: string;
44-
private age: number;
45-
private phone: string;
46-
private address: string;
47-
48-
constructor(builder: UserBuilder) {
49-
this.name = builder.Name;
50-
this.age = builder.Age;
51-
this.phone = builder.Phone;
52-
this.address = builder.Address
53-
}
54-
55-
get Name() {
56-
return this.name;
57-
}
58-
get Age() {
59-
return this.age;
60-
}
61-
get Phone() {
62-
return this.phone;
63-
}
64-
get Address() {
65-
return this.address;
66-
}
67-
}
68-
69-
}
70-
71-
(function main() {
72-
var u: BuilderPattern.User = new BuilderPattern.UserBuilder("Jancsi")
73-
.setAge(12)
74-
.setPhone("0123456789")
75-
.setAddress("asdf")
76-
.build();
77-
console.log(u.Name + " " + u.Age + " " + u.Phone + " " + u.Address);
78-
}());
1+
namespace BuilderPattern {
2+
export class UserBuilder {
3+
private name: string;
4+
private age: number;
5+
private phone: string;
6+
private address: string;
7+
8+
constructor(name: string) {
9+
this.name = name;
10+
}
11+
12+
get Name() {
13+
return this.name;
14+
}
15+
setAge(value: number): UserBuilder {
16+
this.age = value;
17+
return this;
18+
}
19+
get Age() {
20+
return this.age;
21+
}
22+
setPhone(value: string): UserBuilder {
23+
this.phone = value;
24+
return this;
25+
}
26+
get Phone() {
27+
return this.phone;
28+
}
29+
setAddress(value: string): UserBuilder {
30+
this.address = value;
31+
return this;
32+
}
33+
get Address() {
34+
return this.address;
35+
}
36+
37+
build(): User {
38+
return new User(this);
39+
}
40+
}
41+
42+
export class User {
43+
private name: string;
44+
private age: number;
45+
private phone: string;
46+
private address: string;
47+
48+
constructor(builder: UserBuilder) {
49+
this.name = builder.Name;
50+
this.age = builder.Age;
51+
this.phone = builder.Phone;
52+
this.address = builder.Address
53+
}
54+
55+
get Name() {
56+
return this.name;
57+
}
58+
get Age() {
59+
return this.age;
60+
}
61+
get Phone() {
62+
return this.phone;
63+
}
64+
get Address() {
65+
return this.address;
66+
}
67+
}
68+
69+
}

builder/demo.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// <reference path="builder.ts" />
2+
3+
namespace BuilderPattern {
4+
export namespace Demo {
5+
export function show() : void {
6+
var u: BuilderPattern.User = new BuilderPattern.UserBuilder("Jancsi")
7+
.setAge(12)
8+
.setPhone("0123456789")
9+
.setAddress("asdf")
10+
.build();
11+
console.log(u.Name + " " + u.Age + " " + u.Phone + " " + u.Address);
12+
}
13+
}
14+
}
15+

0 commit comments

Comments
 (0)