-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathdata-view.ts
80 lines (64 loc) · 1.83 KB
/
data-view.ts
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import { IDataModel } from './data-model'
export interface IDataView {
// A Subject Interface
notify(data: number[]): void
draw(data: number[]): void
delete(): void
}
export class BarGraphView implements IDataView {
// A concrete observer
#observable: IDataModel
#id: number
constructor(observable: IDataModel) {
this.#observable = observable
this.#id = this.#observable.subscribe(this)
}
notify(data: number[]): void {
console.log(`BarGraph, id:${this.#id}`)
this.draw(data)
}
draw(data: number[]): void {
console.log(`Drawing a Bar graph using data:${JSON.stringify(data)}`)
}
delete(): void {
this.#observable.unsubscribe(this.#id)
}
}
export class PieGraphView implements IDataView {
// A concrete observer
#observable: IDataModel
#id: number
constructor(observable: IDataModel) {
this.#observable = observable
this.#id = this.#observable.subscribe(this)
}
notify(data: number[]): void {
console.log(`PieGraph, id:${this.#id}`)
this.draw(data)
}
draw(data: number[]): void {
console.log(`Drawing a Pie graph using data:${data}`)
}
delete(): void {
this.#observable.unsubscribe(this.#id)
}
}
export class TableView implements IDataView {
// A concrete observer
#observable: IDataModel
#id: number
constructor(observable: IDataModel) {
this.#observable = observable
this.#id = this.#observable.subscribe(this)
}
notify(data: number[]): void {
console.log(`TableView, id:${this.#id}`)
this.draw(data)
}
draw(data: number[]): void {
console.log(`Drawing a Table using data:${JSON.stringify(data)}`)
}
delete(): void {
this.#observable.unsubscribe(this.#id)
}
}