-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathDependency_Injection.js
45 lines (42 loc) · 1015 Bytes
/
Dependency_Injection.js
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
// Imperative Way
// class Calculator {
// sum(a, b) {
// return a + b;
// }
// }
//
// class Receipt {
// constructor(calculator) {
// this.calc = calculator;
// }
// print(itemA, itemB) {
// const total = this.calc.sum(itemA, itemB);
// console.log(`total receipt $ ${total}`);
// }
// }
//
// const pizza = 6.00, beer = 5.00,
// calc = new Calculator(),
// receipt = new Receipt(calc);
// receipt.print(pizza, beer);
// Reactive Way
// class Calculator {
// constructor(itemA, itemB) {
// const obs = Rx.Observable.of(itemA, itemB);
// const sum = obs.reduce((acc, item) => acc + item);
// return {
// observable: sum
// }
// }
// }
//
// class Receipt {
// constructor(observable$) {
// observable$.subscribe(value => console.log(`total receipt $ ${value}`));
// }
// }
//
// const pizza = 6.00, beer = 5.00,
// calc = new Calculator(pizza, beer),
// receipt = new Receipt(calc.observable);
// receipt.print(pizza, beer);