typescript: model

 

 

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
/**
 * TypeScript 实体类 Model
 * 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;
    }
 
}
   
/**
 * 继承
 */
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();
*/
 
/**
 * 接口
 */
interface Builder {
    /**
     *
     * @param seats
     */
    setSeats(seats: number): this;
    /**
     *
     * @param engine
     */
    setEngine(engine: string): this;
 
  }
   
 
/**
 * 继承 Builder
 */
export class CarBuilder implements Builder {
 
    /**
     * 车信息类
     */
    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;
    }
    /**
     * 得到实体
     * @returns 返回车信息类
     */
    public getResult(): Car {
      return this.car;
    }
  }
   
  class DuDirector {
 
    public buildFerrari(): Car {
      return new CarBuilder().setSeats(2).setEngine("V-12").getResult();
    }
   
    public buildToyota(): Car {
      return new CarBuilder().setSeats(7).setEngine("V-6").getResult();
    }
   
    public buildHonda(): Motorcycle {
      return new MotorcycleBuilder().setSeats(2).setEngine("V-4").getResult();
    }
   
    public buildYamaha(): Motorcycle {
      return new MotorcycleBuilder().setSeats(1).setEngine("V-2").getResult();
    }
  }
 
/* */
class MotorcycleBuilder implements Builder {
 
    private motorcycle: Motorcycle;
   
    constructor() {
      this.motorcycle = new Motorcycle();
    }
   
    public setSeats(seats: number): this {
      this.motorcycle.setSeats(seats);
      return this;
    }
   
    public setEngine(engine: string): this {
      this.motorcycle.setEngine(engine);
      return this;
    }
   
    public getResult(): Motorcycle {
      return this.motorcycle;
    }
  }
  
/**/
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()
  .setSeats(2)
  .setEngine("V-4")
  .getResult();

  

 

 

tsconfig.json

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
{
    "compilerOptions": {
    
      "target": "ES5",
      "module": "CommonJS",
      "outDir": "./dist",
      "rootDir": "./src",
      "resolveJsonModule": true,
      "composite": true// required on the dependency project for references to work https://github.com/microsoft/TypeScript/issues/30693
      "sourceMap": true,
      "strictPropertyInitialization": false
      /*"emitDecoratorMetadata": false,
      "experimentalDecorators": true,
      "removeComments": false,
      "strict": true,
      "strictNullChecks": true,*/
      //"declaration": false
      /*
      "strict": true,
      "experimentalDecorators": true,
      "useDefineForClassFields": false,
      "skipLibCheck": true,
      "forceConsistentCasingInFileNames": true,
      "esModuleInterop": true,
      "moduleResolution": "node",
        // Tells TypeScript to read JS files, as normally they are ignored as source files
        "allowJs": true,
        // Generate d.ts files
        "declaration": true,
        // go to js file when using IDE functions like "Go to Definition" in VSCode
        "declarationMap": true,
         // This compiler run should only output d.ts files
        "emitDeclarationOnly": true
        "lib": [
          "es2017",
          "es2018"
        ]*/
 
    },
    "includes": [
    "src/**/*.ts",
    "other-src/**/*.ts"
    ],
    "exclude":[
      "rollup.config.js",
      "test",
      "dist",
      "node_modules",
    ],
  }

  

 

https://github.com/FreekPaans/PPPPayrollCaseStudy
https://github.com/bysoul/Payroll
https://github.com/GeekChao/Agile-Software-Development
https://www.typescriptlang.org/docs/handbook/modules.html
https://www.typescriptlang.org/docs/handbook/classes.html
https://www.koderhq.com/tutorial/typescript/get-set/
https://dev.to/jmalvarez/builder-pattern-in-typescript-3on0

 

posted @   ®Geovin Du Dream Park™  阅读(19)  评论(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
点击右上角即可分享
微信分享提示