forked from zoltantothcom/Design-Patterns-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbehavioral_strategy.js
More file actions
66 lines (53 loc) · 1.43 KB
/
behavioral_strategy.js
File metadata and controls
66 lines (53 loc) · 1.43 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
const STRATEGY = {
id: 'strategy',
name: 'Strategy',
type: 'behavioral',
hint: 'Encapsulates an algorithm inside a class',
definition: `Define a family of algorithms, encapsulate each one, and make them interchangeable.
Strategy lets the algorithm vary independently from clients that use it.`,
when: `you have many classes that differ in their behaviour.
Strategies allow to configure a class with one of many behaviours`,
codeES5: `function ShoppingCart(discount) {
this.discount = discount;
this.amount = 0;
}
ShoppingCart.prototype.setAmount = function(amount) {
this.amount = amount;
};
ShoppingCart.prototype.checkout = function() {
return this.discount(this.amount);
};
function guestPattern(amount) {
return amount;
}
function regularPattern(amount) {
return amount * 0.9;
}
function premiumPattern(amount) {
return amount * 0.8;
}
module.exports = [ShoppingCart, guestPattern, regularPattern, premiumPattern];`,
codeES6: `class ShoppingCart {
constructor(discount) {
this.discount = discount;
this.amount = 0;
}
checkout() {
return this.discount(this.amount);
}
setAmount(amount) {
this.amount = amount;
}
}
function guestPattern(amount) {
return amount;
}
function regularPattern(amount) {
return amount * 0.9;
}
function premiumPattern(amount) {
return amount * 0.8;
}
export { ShoppingCart, guestPattern, regularPattern, premiumPattern };`
};
export default STRATEGY;