forked from Reaper622/JavaScript-DesignPatterns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdapter.js
More file actions
51 lines (43 loc) · 924 Bytes
/
Adapter.js
File metadata and controls
51 lines (43 loc) · 924 Bytes
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
class ThinkPad {
charge() {
console.log('ThinkPad 开始充电');
}
}
class MacBook {
charge() {
console.log('MacBook 开始充电')
}
}
// 电源充电
function PowerToCharge(laptop) {
if(laptop.charge instanceof Function) {
laptop.charge()
}
}
PowerToCharge(new ThinkPad()) // ThinkPad开始充电
PowerToCharge(new MacBook()) // MacBook开始充电
class ThinkPad {
charge() {
console.log('ThinkPad 开始充电');
}
}
class MacBook {
type_cCharge() {
console.log('MacBook 开始充电')
}
}
// 定义适配器类,来实现对MacBook的转接
class TypeCToDp {
charge() {
let macbook = new MacBook();
return macbook.type_cCharge()
}
}
// 电源充电
function PowerToCharge(laptop) {
if(laptop.charge instanceof Function) {
laptop.charge()
}
}
PowerToCharge(new ThinkPad()) // ThinkPad开始充电
PowerToCharge(new TypeCToDp()) // MacBook开始充电