-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path0-all.js
More file actions
63 lines (50 loc) · 1.56 KB
/
0-all.js
File metadata and controls
63 lines (50 loc) · 1.56 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
'use strict';
class Amount {
// Abstraction with mutable state
#value = 0; // Private state, Hiding, Encapsulation
constructor(initial = 0) {
this.#value = initial;
}
update(delta) {
// Public interface: method
this.#value += delta; // Implementation, Encapsulation
return this.#value;
}
get value() {
// Public interface: getter
return this.#value; // Implementation, Encapsulation
}
}
class Spending extends EventTarget {
// Inheritance
#amount = new Amount(); // Structural Composition
update(delta = 1) {
const value = this.#amount.update(delta); // Delegation
const detail = { value, delta };
this.dispatchEvent(new CustomEvent('change', { detail }));
return value;
}
get value() {
return this.#amount.value; // Delegation
}
}
const config = { available: 10000 }; // Immutable configuration
const budget = new Amount(config.available); // Value Object
const spending = new Spending(); // Entity
const balance = new Amount(config.available);
const report = (purchase, logger) => {
// Dependency Injection
for (const [key, instance] of Object.entries(purchase)) {
logger(`${key}: ${instance.value}`); // Polymorphism
}
};
spending.addEventListener('change', (event) => {
const { value, delta } = event.detail;
balance.update(-delta); // Tell Don't Ask
console.log({ event: event.type, value, delta });
});
spending.update(1023);
spending.update(1507);
spending.update(1010);
const purchase = { budget, spending, balance }; // Aggregation
report(purchase, console.log); // Dependency Injection