1
- //创建2台车:一台是宝马车架、宝马轮子、宝马引擎的汽车,另一台是宝马车架、宝马轮子、奔驰引擎的汽车
1
+ //创建2台车:一台是宝马车架、宝马轮子、宝马引擎的汽车,另一台是宝马车架、宝马轮子、奔驰引擎的汽车
2
2
3
- //汽车类
3
+ //汽车类
4
4
class Car {
5
5
private carBody : String
6
6
private wheel : String
@@ -15,8 +15,8 @@ class Car {
15
15
this . engine = engine
16
16
}
17
17
public show ( ) : void {
18
- //显示产品的特性
19
- console . log ( '汽车的组成分别为:' , this . carBody , this . wheel , this . engine )
18
+ //显示产品的特性
19
+ console . log ( '汽车的组成分别为:' , this . carBody , this . wheel , this . engine )
20
20
}
21
21
}
22
22
@@ -29,7 +29,7 @@ abstract class Builder {
29
29
return this . car
30
30
}
31
31
}
32
- //汽车1的建造者
32
+ //汽车1的建造者
33
33
class ConcreteBuilder extends Builder {
34
34
constructor ( ) {
35
35
super ( )
@@ -44,16 +44,16 @@ class ConcreteBuilder extends Builder {
44
44
this . car . setEngine ( "BMWEngine" )
45
45
}
46
46
}
47
- //汽车2的建造者
47
+ //汽车2的建造者
48
48
class ConcreteBuilder1 extends Builder {
49
49
constructor ( ) {
50
50
super ( )
51
51
}
52
52
public buildCarBody ( ) : void {
53
- this . car . setCarBody ( "BMWCarBody " )
53
+ this . car . setCarBody ( "BenzCarBody " )
54
54
}
55
55
public buildWheel ( ) : void {
56
- this . car . setWheel ( "BMWWheel " )
56
+ this . car . setWheel ( "BenzWheel " )
57
57
}
58
58
public buildEngine ( ) : void {
59
59
this . car . setEngine ( "BenzEngine" )
@@ -68,19 +68,19 @@ class Director {
68
68
}
69
69
}
70
70
71
- //测试
71
+ //测试
72
72
class Client {
73
73
public static main ( ) : void {
74
- //第一台汽车:创建者builder
74
+ //第一台汽车:创建者builder
75
75
const builder : Builder = new ConcreteBuilder ( )
76
76
const car : Car = Director . getProduct ( builder )
77
77
car . show ( )
78
- //第二台汽车:创建者builder1,调整部件
78
+ //第二台汽车:创建者builder1,调整部件
79
79
const builder1 : Builder = new ConcreteBuilder1 ( )
80
80
const car1 : Car = Director . getProduct ( builder1 )
81
81
car1 . show ( )
82
82
}
83
83
}
84
84
Client . main ( )
85
85
86
- //小结:想创建一个宝马车架、奥迪车轮、奔驰引擎的汽车,也只需要新建一个建造者类,符合开闭原则
86
+ //小结:想创建一个宝马车架、奥迪车轮、奔驰引擎的汽车,也只需要新建一个建造者类,符合开闭原则
0 commit comments