-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathBridgePattern.js
More file actions
121 lines (97 loc) · 1.94 KB
/
Copy pathBridgePattern.js
File metadata and controls
121 lines (97 loc) · 1.94 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"use strict";
/**
* Bridge Pattern - Allows two components to work with each other having own
* interface.
*
* Example - Gestures and Mouse are different input devices but they shares
* the same set of methods. These methods are sent to two different output
* devices (Screen and Audio) but with same set of methods. Bridge pattern
* allows any input device to work with any output device
*
* Reference: http://www.dofactory.com/javascript/bridge-design-pattern
*
*/
function forceRequiredMethods(_class, methods) {
for (let eachMethod of methods) {
if(_class[eachMethod] === undefined || typeof _class[eachMethod] !== 'function') {
throw new TypeError(eachMethod + ' is required and has to be overriden.');
}
}
}
class InputDevice {
constructor(output) {
this.output = output;
}
}
class Gestures extends InputDevice {
tap() {
this.output.click();
}
swipe() {
this.output.move();
}
pan() {
this.output.drag();
}
pinch() {
this.output.zoom();
}
}
class Mouse extends InputDevice {
click() {
this.output.click();
}
move() {
this.output.move();
}
down() {
this.output.drag();
}
wheel() {
this.output.zoom();
}
}
class OutputDevice {
constructor() {
let _class = this;
forceRequiredMethods(_class, ['click', 'move', 'drag', 'zoom']);
}
}
class Screen extends OutputDevice {
click() {
console.log('Screen select');
}
move() {
console.log('Screen move');
}
drag() {
console.log('Screen drag');
}
zoom() {
console.log('Screen zoom');
}
}
class Audio extends OutputDevice {
click() {
console.log('Sound: Ting');
}
move() {
console.log('Sound: Swoosh');
}
drag() {
console.log('Sound: Eeeee');
}
zoom() {
console.log('Sound: Vrooom');
}
}
var screen = new Screen();
var audio = new Audio();
var hand = new Gestures(screen);
var mouse = new Mouse(audio);
hand.tap();
hand.swipe();
hand.pinch();
mouse.click();
mouse.move();
mouse.wheel();