forked from zoltantothcom/Design-Patterns-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructural_bridge.js
More file actions
85 lines (69 loc) · 1.55 KB
/
structural_bridge.js
File metadata and controls
85 lines (69 loc) · 1.55 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const BRIDGE = {
id: 'bridge',
name: 'Bridge',
type: 'structural',
hint: 'Separates an object’s interface from its implementation',
definition: `Decouple an abstraction from its implementation so that the two can vary independently.`,
when: `you want to avoid binding between abstraction and its implementation if, for example,
each of them must be selected in runtime`,
codeES5: `function EpsonPrinter(ink) {
this.ink = ink();
}
EpsonPrinter.prototype.print = function() {
return 'Printer: Epson, Ink: ' + this.ink;
};
function HPprinter(ink) {
this.ink = ink();
}
HPprinter.prototype.print = function() {
return 'Printer: HP, Ink: ' + this.ink;
};
function acrylicInk() {
return 'acrylic-based';
}
function alcoholInk() {
return 'alcohol-based';
}
module.exports = [EpsonPrinter, HPprinter, acrylicInk, alcoholInk];`,
codeES6: `class Printer {
constructor(ink) {
this.ink = ink;
}
}
class EpsonPrinter extends Printer {
constructor(ink) {
super(ink);
}
print() {
return 'Printer: Epson, Ink: ' + this.ink.get();
}
}
class HPprinter extends Printer {
constructor(ink) {
super(ink);
}
print() {
return 'Printer: HP, Ink: ' + this.ink.get();
}
}
class Ink {
constructor(type) {
this.type = type;
}
get() {
return this.type;
}
}
class AcrylicInk extends Ink {
constructor() {
super('acrylic-based');
}
}
class AlcoholInk extends Ink {
constructor() {
super('alcohol-based');
}
}
export { EpsonPrinter, HPprinter, AcrylicInk, AlcoholInk };`
};
export default BRIDGE;