-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchouxianggongchang.html
76 lines (71 loc) · 1.85 KB
/
chouxianggongchang.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
<!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>
class MobilePhoneFactory {
// 提供操作系统的借口
createOS() {
throw new Error('抽象工厂方法不允许直接调用,你需要将我重写');
}
createHardWare() {
throw new Error('抽象工厂方法不允许直接调用,你需要将我重写');
}
}
class FakeStarFactory extends MobilePhoneFactory {
createOS() {
return new AppleOS();
}
createHardWare() {
return new MiWare();
}
}
/**
* 操作系统
*/
class OS {
controlHardWare() {
throw new Error('抽象产品方法不允许直接调用,你需要将我重写');
}
}
class AndroidOS extends OS{
controlHardWare() {
console.log('我会用安卓的方法去操作硬件');
}
}
class AppleOS extends OS{
controlHardWare() {
console.log('我会用苹果的方式去操作硬件');
}
}
/**
* 系统硬件
*/
class HardWare {
operateByOrder() {
throw new Error('抽象产品不允许直接调用,你需要将我重写');
}
}
class QualcommHardWare extends HardWare {
operateByOrder() {
console.log('我会用高通的方式去运转');
}
}
class MiWare extends Headers{
operateByOrder() {
console.log('我会用小米的方式去运转');
}
}
const myPhone = new FakeStarFactory();
const myOS = myPhone.createOS();
const myHardWare = myPhone.createHardWare();
myOS.controlHardWare();
myHardWare.operateByOrder();
</script>
</body>
</html>