forked from zoltantothcom/Design-Patterns-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstructural_decorator.js
More file actions
91 lines (73 loc) · 1.63 KB
/
structural_decorator.js
File metadata and controls
91 lines (73 loc) · 1.63 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
const DECORATOR = {
id: 'decorator',
name: 'Decorator',
type: 'structural',
hint: 'Add responsibilities to objects dynamically',
definition: `Attach additional responsibilities to an object dynamically.
Decorators provide a flexible alternative to subclassing for extending functionality.`,
when: `you want to add extensions to an object in runtime without affecting other objects`,
codeES5: `function Pasta() {
this.price = 0;
}
Pasta.prototype.getPrice = function() {
return this.price;
};
function Penne() {
this.price = 8;
}
Penne.prototype = Object.create(Pasta.prototype);
function SaucePattern(pasta) {
this.pasta = pasta;
}
SaucePattern.prototype.getPrice = function() {
return this.pasta.getPrice() + 5;
};
function CheesePattern(pasta) {
this.pasta = pasta;
}
CheesePattern.prototype.getPrice = function() {
return this.pasta.getPrice() + 3;
};
module.exports = [Penne, SaucePattern, CheesePattern];`,
codeES6: `class Pasta {
constructor() {
this.price = 0;
}
getPrice() {
return this.price;
}
}
class Penne extends Pasta {
constructor() {
super();
this.price = 8;
}
}
class PastaPattern extends Pasta {
constructor(pasta) {
super();
this.pasta = pasta;
}
getPrice() {
return this.pasta.getPrice();
}
}
class SaucePattern extends PastaPattern {
constructor(pasta) {
super(pasta);
}
getPrice() {
return super.getPrice() + 5;
}
}
class CheesePattern extends PastaPattern {
constructor(pasta) {
super(pasta);
}
getPrice() {
return super.getPrice() + 3;
}
}
export { Penne, SaucePattern, CheesePattern };`
};
export default DECORATOR;