8000 build-docs · wolfwithcode/design-patterns-JS@d676dac · GitHub
[go: up one dir, main page]

Skip to content

Commit d676dac

Browse files
committed
build-docs
1 parent b5b0e3e commit d676dac

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

docs.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,76 @@
3333

3434
## behavioral
3535
### Chain Of Resp
36+
##### chain-of-resp-es6.js
37+
```Javascript
38+
class ShoppingCart {
39+
constructor() {
40+
this.products = [];
41+
}
42+
43+
addProduct(p) {
44+
this.products.push(p);
45+
};
46+
}
47+
48+
class Discount {
49+
calc(products) {
50+
let ndiscount = new NumberDiscount();
51+
let pdiscount = new PriceDiscount();
52+
let none = new NoneDiscount();
53+
ndiscount.setNext(pdiscount);
54+
pdiscount.setNext(none);
55+
return ndiscount.exec(products);
56+
};
57+
}
58+
59+
class NumberDiscount {
60+
constructor() {
61+
this.next = null;
62+
}
63+
64+
setNext(fn) {
65+
this.next = fn;
66+
};
67+
68+
exec(products) {
69+
let result = 0;
70+
if (products.length > 3)
71+
result = 0.05;
72+
73+
return result + this.next.exec(products);
74+
};
75+
}
76+
77+
class PriceDiscount{
78+
constructor() {
79+
this.next = null;
80+
}
81+
82+
setNext(fn) {
83+
this.next = fn;
84+
};
85+
86+
exec(products) {
87+
let result = 0;
88+
let total = products.reduce((a, b)=> a + b);
89+
90+
if (total >= 500)
91+
result = 0.1;
92+
93+
return result + this.next.exec(products);
94+
};
95+
}
96+
97+
class NoneDiscount {
98+
exec() {
99+
return 0;
100+
};
101+
}
102+
103+
export { ShoppingCart , Discount };
104+
105+
```
36106
##### chain-of-resp.js
37107
```Javascript
38108
function ShoppingCart() {

0 commit comments

Comments
 (0)
0