forked from zoltantothcom/Design-Patterns-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructural_adapter.js
More file actions
67 lines (54 loc) · 1.25 KB
/
structural_adapter.js
File metadata and controls
67 lines (54 loc) · 1.25 KB
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
const ADAPTER = {
id: 'adapter',
name: 'Adapter',
type: 'structural',
hint: `Match interfaces of different classes`,
definition: `Convert the interface of a class into another interface clients expect.
Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.`,
when: `you want to use existing class but its interface does not match the one you need`,
codeES5: `function Soldier(lvl) {
this.lvl = lvl;
}
Soldier.prototype.attack = function() {
return this.lvl * 1;
};
function Jedi(lvl) {
this.lvl = lvl;
}
Jedi.prototype.attackWithSaber = function() {
return this.lvl * 100;
};
function JediPattern(jedi) {
this.jedi = jedi;
}
JediPattern.prototype.attack = function() {
return this.jedi.attackWithSaber();
};
module.exports = [Soldier, Jedi, JediPattern];`,
codeES6: `class Soldier {
constructor(level) {
this.level = level;
}
attack() {
return this.level * 1;
}
}
class Jedi {
constructor(level) {
this.level = level;
}
attackWithSaber() {
return this.level * 100;
}
}
class JediPattern {
constructor(jedi) {
this.jedi = jedi;
}
attack() {
return this.jedi.attackWithSaber();
}
}
export { Soldier, Jedi, JediPattern };`
};
export default ADAPTER;