|
| 1 | +const CHAIN_OF_RESPONSIBILITY = { |
| 2 | + id: 'chain_of_responsibility', |
| 3 | + name: 'Chain of Responsibility', |
| 4 | + type: 'behavioral', |
| 5 | + hint: 'delegates commands to a chain of processing objects', |
| 6 | + codeES5: `function ShoppingCart() { |
| 7 | + this.products = []; |
| 8 | +
|
| 9 | + this.addProduct = function(p) { |
| 10 | + this.products.push(p); |
| 11 | + }; |
| 12 | +} |
| 13 | +
|
| 14 | +function Discount() { |
| 15 | + this.calc = function(products) { |
| 16 | + var ndiscount = new NumberDiscount(); |
| 17 | + var pdiscount = new PriceDiscount(); |
| 18 | + var none = new NoneDiscount(); |
| 19 | +
|
| 20 | + ndiscount.setNext(pdiscount); |
| 21 | + pdiscount.setNext(none); |
| 22 | +
|
| 23 | + return ndiscount.exec(products); |
| 24 | + }; |
| 25 | +} |
| 26 | +
|
| 27 | +function NumberDiscount() { |
| 28 | + this.next = null; |
| 29 | + this.setNext = function(fn) { |
| 30 | + this.next = fn; |
| 31 | + }; |
| 32 | +
|
| 33 | + this.exec = function(products) { |
| 34 | + var result = 0; |
| 35 | + if (products.length > 3) result = 0.05; |
| 36 | +
|
| 37 | + return result + this.next.exec(products); |
| 38 | + }; |
| 39 | +} |
| 40 | +
|
| 41 | +function PriceDiscount() { |
| 42 | + this.next = null; |
| 43 | + this.setNext = function(fn) { |
| 44 | + this.next = fn; |
| 45 | + }; |
| 46 | + this.exec = function(products) { |
| 47 | + var result = 0; |
| 48 | + var total = products.reduce(function(a, b) { |
| 49 | + return a + b; |
| 50 | + }); |
| 51 | +
|
| 52 | + if (total >= 500) result = 0.1; |
| 53 | +
|
| 54 | + return result + this.next.exec(products); |
| 55 | + }; |
| 56 | +} |
| 57 | +
|
| 58 | +function NoneDiscount() { |
| 59 | + this.exec = function() { |
| 60 | + return 0; |
| 61 | + }; |
| 62 | +} |
| 63 | +
|
| 64 | +module.exports = [ShoppingCart, Discount];`, |
| 65 | + codeES6: `class ShoppingCart { |
| 66 | + constructor() { |
| 67 | + this.products = []; |
| 68 | + } |
| 69 | +
|
| 70 | + addProduct(p) { |
| 71 | + this.products.push(p); |
| 72 | + } |
| 73 | +} |
| 74 | +
|
| 75 | +class Discount { |
| 76 | + calc(products) { |
| 77 | + let ndiscount = new NumberDiscount(); |
| 78 | + let pdiscount = new PriceDiscount(); |
| 79 | + let none = new NoneDiscount(); |
| 80 | + ndiscount.setNext(pdiscount); |
| 81 | + pdiscount.setNext(none); |
| 82 | +
|
| 83 | + return ndiscount.exec(products); |
| 84 | + } |
| 85 | +} |
| 86 | +
|
| 87 | +class NumberDiscount { |
| 88 | + constructor() { |
| 89 | + this.next = null; |
| 90 | + } |
| 91 | +
|
| 92 | + setNext(fn) { |
| 93 | + this.next = fn; |
| 94 | + } |
| 95 | +
|
| 96 | + exec(products) { |
| 97 | + let result = 0; |
| 98 | + if (products.length > 3) result = 0.05; |
| 99 | +
|
| 100 | + return result + this.next.exec(products); |
| 101 | + } |
| 102 | +} |
| 103 | +
|
| 104 | +class PriceDiscount { |
| 105 | + constructor() { |
| 106 | + this.next = null; |
| 107 | + } |
| 108 | +
|
| 109 | + setNext(fn) { |
| 110 | + this.next = fn; |
| 111 | + } |
| 112 | +
|
| 113 | + exec(products) { |
| 114 | + let result = 0; |
| 115 | + let total = products.reduce((a, b) => a + b); |
| 116 | +
|
| 117 | + if (total >= 500) result = 0.1; |
| 118 | +
|
| 119 | + return result + this.next.exec(products); |
| 120 | + } |
| 121 | +} |
| 122 | +
|
| 123 | +class NoneDiscount { |
| 124 | + exec() { |
| 125 | + return 0; |
| 126 | + } |
| 127 | +} |
| 128 | +
|
| 129 | +export { ShoppingCart, Discount };` |
| 130 | +}; |
| 131 | + |
| 132 | +export default CHAIN_OF_RESPONSIBILITY; |
0 commit comments