-
Notifications
You must be signed in to change notification settings - Fork 0
/
factory.html
89 lines (84 loc) · 2.03 KB
/
factory.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>简单工厂</title>
</head>
<body>
<script type="text/javascript">
// 将实例化车子交给工厂对象 云端
var BicycleFactory = {
createBicycle: function(model) {
var bicycle;
switch (model) {
case 'The Speedster':
bicycle = new Speedster();
break;
case 'The Lowrider':
bicycle = new Lowrider();
break;
case 'Giant':
bicycle = new Giant();
break;
case 'The Comfort Cruiser':
default:
bicycle = new ComfortCruiser();
}
return bicycle;
}
}
//自行车商店
var BicycleShop = function() {
}
BicycleShop.prototype = {
sellBicycle: function(model) {
var bicycle = BicycleFactory.createBicycle(model);
bicycle.assemble();
bicycle.wash();
bicycle.ride();
bicycle.repair();
return bicycle;
}
}
function Bicycle() {
}
Bicycle.prototype = {
wash: function() {
console.log('清洗车');
},
assemble: function() {
console.log('组装车');
},
ride: function() {
console.log('包骑三年');
},
repair: function() {
console.log('保修一年');
}
}
function Speedster() {
Bicycle.apply(this, arguments);
this.brand = 'The Speedster';
this.price = 2014;
}
Speedster.prototype = new Bicycle();
function ComfortCruiser() {
Bicycle.apply(this, arguments);
this.brand = 'The Comfort Cruiser';
this.price = 3000;
}
ComfortCruiser.prototype = new Bicycle();
function Giant() {
Bicycle.apply(this, arguments);
this.brand = 'The Giant';
this.price = 9999;
}
Giant.prototype = new Bicycle();
var californiaCruisers = new BicycleShop();
var yourNewBike = californiaCruisers.sellBicycle('Giant');
console.log(yourNewBike.brand);
</script>
</body>
</html>