-
Notifications
You must be signed in to change notification settings - Fork 0
/
typescript.ts
49 lines (38 loc) · 1.15 KB
/
typescript.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
// Observer: Define una dependencia de uno a muchos entre objetos.
interface Observer {
update(subject: Subject): void;
}
class Subject {
private observers: Observer[] = [];
private state: string;
public getState(): string {
return this.state;
}
public setState(state: string): void {
this.state = state;
this.notifyObservers();
}
public addObserver(observer: Observer): void {
this.observers.push(observer);
}
public removeObserver(observer: Observer): void {
this.observers = this.observers.filter((obs) => obs !== observer);
}
public notifyObservers(): void {
this.observers.forEach((observer) => observer.update(this));
}
}
class ConcreteObserver implements Observer {
public update(subject: Subject): void {
console.log(
`Observer: Reacted to the state change to ${subject.getState()}`
);
}
}
const subject = new Subject();
const observer1 = new ConcreteObserver();
const observer2 = new ConcreteObserver();
subject.addObserver(observer1);
subject.addObserver(observer2);
subject.setState("State1"); // Observer: Reacted to the state change to State1
subject.setState("State2"); // Observer: Reacted to the state change to State2