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+ }
0 commit comments