forked from zoltantothcom/Design-Patterns-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbehavioral_template.js
More file actions
70 lines (55 loc) · 1.48 KB
/
behavioral_template.js
File metadata and controls
70 lines (55 loc) · 1.48 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
const TEMPLATE = {
id: 'template',
name: 'Template',
type: 'behavioral',
hint: 'Defer the exact steps of an algorithm to a subclass',
definition: `Define the skeleton of an algorithm in an operation, deferring some steps to subclasses.
Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.`,
when: `you have to define steps of the algorithm once and let subclasses to implement its behaviour`,
codeES5: `function Tax() {}
Tax.prototype.calc = function(value) {
if (value >= 1000) value = this.overThousand(value);
return this.complementaryFee(value);
};
Tax.prototype.complementaryFee = function(value) {
return value + 10;
};
function Tax1() {}
Tax1.prototype = Object.create(Tax.prototype);
Tax1.prototype.overThousand = function(value) {
return value * 1.1;
};
function Tax2() {}
Tax2.prototype = Object.create(Tax.prototype);
Tax2.prototype.overThousand = function(value) {
return value * 1.2;
};
module.exports = [Tax1, Tax2];`,
codeES6: `class Tax {
calc(value) {
if (value >= 1000) value = this.overThousand(value);
return this.complementaryFee(value);
}
complementaryFee(value) {
return value + 10;
}
}
class Tax1 extends Tax {
constructor() {
super();
}
overThousand(value) {
return value * 1.1;
}
}
class Tax2 extends Tax {
constructor() {
super();
}
overThousand(value) {
return value * 1.2;
}
}
export { Tax1, Tax2 };`
};
export default TEMPLATE;