typescript: Builder Pattern

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/**
 * TypeScript 实体类 Model
 * Builder Pattern
 * 生成器是一种创建型设计模式, 使你能够分步骤创建复杂对象。
 * https://stackoverflow.com/questions/12827266/get-and-set-in-typescript
 * https://github.com/Microsoft/TypeScript/wiki/Coding-guidelines
 */
class UserInfo {
    id!: number;
    userName!: string;
    email!: string;
  }
 
 
  /**
   * 车辆实体类
   */
  class Car { //export
 
    /**
     * 序号
     */
    id!: number;
    /**
     * 座位
     */
    seats!: number;
    /**
     * 发动机
     */
    engine!: string;
 
   /* constructor(id, seats,engine){
        this.id = id;
        this.seats = seats;
        this.engine=engine;
    }*/
 
    /**
     * 序号
     * @returns  返回序号
     */
    public  getId(): number { //get
        return this.id;
    }
 
    /**
     * 座位数
     * @returns 返回座位数
     */
    public  getSeats(): number { //get
        return this.seats;
    }
    /**
     * 发动机
     * @returns 返回发动机型号名称
     */
    public  getEngine(): string { //get
        return this.engine;
    }
    /**
     * 设置座位数
     * @param seats 输入数字座位数
     */
    public setSeats(seats: number) //set
    {
        this.seats=seats;
    }
    /**
     * 设置发动机型号
     * @param engine 输入型号名称
     */
    public setEngine(engine: string) //set
    {
        this.engine=engine;
    }
    /**
     *
     * @param id
     */
    public setId(id:number)
    {
      this.id=id;
    }
 
}
   
/**
 * 继承
 */
class Motorcycle extends Car
{
   /*  id!: number;
    seats!: number;
    engine!: string;
 
   constructor(id, seats,engine){
        this.id = id;
        this.seats = seats;
        this.engine=engine;
    }
 
    public  getId(): number { //get
        return this.id;
    }
 
    public  getSeats(): number { //get
        return this.seats;
    }
 
    public  getEngine(): string { //get
        return this.engine;
    }
 
    public setSeats(seats: number) //set
    {
        this.seats=seats;
    }
 
    public setEngine(engine: string) //set
    {
        this.engine=engine;
    }*/
 
}
/*
  interface DuBuilder<Car>()
    .id(1)
    .setSeats("")
    .setEngine("")
    .build();
*/
 
/**
 * 接口extends Car
 */
interface CBuilder {
 
 
    /**
     *
     * @param seats
     */
    setSeats(seats: number): this;
    /**
     *
     * @param engine
     */
    setEngine(engine: string): this;
 
    /**
     *
     * @param id
     */
    setId(id:number):this;
 
  }
   
 
/**
 * 继承 Builder
 */
class CarBuilder implements CBuilder {
 
    /**
     * 车信息类
     */
    private car: Car;
   
    /**
     * 实例化
     */
    constructor() {
      this.car = new Car();
    }
    /**
     * 设置座位数
     * @param seats 座位号
     * @returns 返回座位号
     */
    public setSeats(seats: number): this {
      //this.car.setSeats(seats);
     
      this.car.setSeats(seats);
      return this;
    }
    /**
     * 设置发动机型号
     * @param engine 发动机型号名称
     * @returns
     */
    public setEngine(engine: string): this {
      this.car.setEngine(engine);
      return this;
    }
    /**
     * id 序号
     * @param id
     * @returns
     */
    public setId(id:number):this{
      this.car.setId(id);
      return this;
    }
    /**
     * 得到实体
     * @returns 返回车信息类
     */
    public getResult(): Car {
      return this.car;
    }
  }
   
 
/**
 *
 *  */
class MotorcycleBuilder implements CBuilder {
 
    /**
     *
     */
    private motorcycle: Motorcycle;
   
    /**
     *
     */
    constructor() {
      this.motorcycle = new Motorcycle();
    }
    /**
     *
     * @param seats
     * @returns
     */
    public setSeats(seats: number): this {
      this.motorcycle.setSeats(seats);
      return this;
    }
    /**
     *
     * @param engine
     * @returns
     */
    public setEngine(engine: string): this {
      this.motorcycle.setEngine(engine);
      return this;
    }
    /**
     *
     * @param id
     * @returns
     */
    public  setId(id: number): this {
        this.motorcycle.setId(id);
        return this;
    }
    /**
     *
     * @returns
     */
    public getResult(): Motorcycle {
      return this.motorcycle;
    }
  }
  
  /**
   *
   */
  class DuDirector {
 
    /**
     *
     * @returns
     */
    public buildFerrari(): Car {
      return new CarBuilder().setId(1).setSeats(2).setEngine("V-12").getResult();
    }
    /**
     *
     * @returns
     */
    public buildToyota(): Car {
      return new CarBuilder().setId(2).setSeats(7).setEngine("V-6").getResult();
    }
    /**
     *
     * @returns
     */
    public buildHonda(): Motorcycle {
      return new MotorcycleBuilder().setId(3).setSeats(2).setEngine("V-4").getResult();
    }
    /**
     *
     * @returns
     */
    public buildYamaha(): Motorcycle {
      return new MotorcycleBuilder().setId(4).setSeats(1).setEngine("V-2").getResult();
    }
  }
 
 
/**
 *
 */
 
const directorBu = new DuDirector();
 
directorBu.buildFerrari();
directorBu.buildToyota();
 
directorBu.buildHonda();
directorBu.buildYamaha();
 
const car = new CarBuilder().setSeats(2).setEngine("V-12").getResult();
 
const motorcycle = new MotorcycleBuilder()
  .setId(100)
  .setSeats(2)
  .setEngine("V-4")
  .getResult();
 
let pucarid=""+motorcycle.getId();
let pucar1=""+motorcycle.getSeats();
let pucar2=""+motorcycle.getEngine();
 
let messageCar: string = 'Hello World,This is a typescript!,涂聚文 Geovin Du Web';
document.body.innerHTML = messageCar+"<br/>,id:"+pucarid+",座位数:"+pucar1+",发动机型号:"+pucar2+","+car.getSeats()+","+car.getEngine()+","+directorBu.buildFerrari().getEngine()+",TypeScript 生成器方法模式";
console.log(motorcycle.getId());
console.log(motorcycle.getSeats());
console.log(motorcycle.getEngine());

  

调用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <head><title>TypeScript:生成器模式</title>
      <meta name="Description" content="geovindu,涂聚文,Geovin Du"/>
<meta name="Keywords" content="geovindu,涂聚文,Geovin Du"/>
<meta name="author" content="geovindu,涂聚文,Geovin Du"/> 
    </head>
    <body>
        <script src="dist/CarBuilderts.js"></script>
    </body>
</html>

  

输出:

 

posted @   ®Geovin Du Dream Park™  阅读(8)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
历史上的今天:
2022-10-05 CSharp: Facade Pattern in donet core 3
2022-10-05 CSharp: Adapter Pattern in donet core 3
2022-10-05 CSharp: Decorator Pattern in donet core 3
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示