Observer Pattern

Observer Pattern

Observer Pattern is commonly use to implement Pub/Sub model.

IObservable Has-A IObservable

WeatherStation Is-A IObservable

PhoneDisplay Is-A Observer

PhoneDisplay Has-A WeatherStation

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

interface IObservable {
add(Observer)
remove(Observer)
notify(Observer)
}

interface IObserver {
update()
}


class weatherStation implements Observable {

observers: array;

add (observer:IObserver) {

observers.push(observer);
}

remove (observer:IObserver) {
observers.remove(observer);
}

notify () {
observers.forEach(function(observer) {
observer.update();
});
}
}

class phoneDisplay implements Observable {

construct(station:WeatherStation) {
this.station = station;
}

update() {
this.station.getTemp();
//do something
}

}


 Previous
Decorator Decorator
Decorator Pattern Decorator AddOnDecoractor is-A Beverage SolidCoffeeCLass is-A Beverage Decorator is-A AddOnDecoractor
2019-05-15 Ray Zhang
Next 
Strategy Pattern Strategy Pattern
Strategy PatternStrategy Pattern is a commonly used pattern for OOP.The idea is to use Has-A relation replace Is-A relat
2019-05-13 Ray Zhang
  TOC