|
| 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 | + return ndiscount.exec(products); |
| 83 | +} |
| 84 | +} |
| 85 | +
|
| 86 | +class NumberDiscount { |
| 87 | +constructor() { |
| 88 | + this.next = null; |
| 89 | +} |
| 90 | +
|
| 91 | +setNext(fn) { |
| 92 | + this.next = fn; |
| 93 | +} |
| 94 | +
|
| 95 | +exec(products) { |
| 96 | + let result = 0; |
| 97 | + if (products.length > 3) result = 0.05; |
| 98 | +
|
| 99 | + return result + this.next.exec(products); |
| 100 | +} |
| 101 | +} |
| 102 | +
|
| 103 | +class PriceDiscount { |
| 104 | +constructor() { |
| 105 | + this.next = null; |
| 106 | +} |
| 107 | +
|
| 108 | +setNext(fn) { |
| 109 | + this.next = fn; |
| 110 | +} |
| 111 | +
|
| 112 | +exec(products) { |
| 113 | + let result = 0; |
| 114 | + let total = products.reduce((a, b) => a + b); |
| 115 | +
|
| 116 | + if (total >= 500) result = 0.1; |
| 117 | +
|
| 118 | + return result + this.next.exec(products); |
| 119 | +} |
| 120 | +} |
| 121 | +
|
| 122 | +class NoneDiscount { |
| 123 | +exec() { |
| 124 | + return 0; |
| 125 | +} |
| 126 | +} |
| 127 | +
|
| 128 | +export { ShoppingCart, Discount };` |
| 129 | +}; |
| 130 | + |
| 131 | +export default CHAIN_OF_RESPONSIBILITY; |
0 commit comments