Please enable JavaScript.
Coggle requires JavaScript to display documents.
OBSERVER PATTERN - Coggle Diagram
OBSERVER PATTERN
SCENARIO
IObservable
-
WeatherStation
- IS A IObservable
- Mesure the temperature
- Notify
IObserver
PhoneDisplay
- IS A IObservable
- HAS A WeatherStation #
WindowDisplay
- IS A IObservable
- HAS A WeatherStation #
TIP: if we use inheritance a child class can only have a single parent, with interfaces our system is more flexible
CODE EXAMPLE
WORKFLOW:
- Instantiate a Weather Station
- Instantiate OBSERVERS and pass the OBSERVABLE
- We ADD the observer to the Weather Station
-
-
-
-
-
DEFINITION
INTENT:
- Object A changes state
- Object B needs to know about Object A when the state has changed
Push
- Object A is responsible to tell other subscribers that it's changed.
How?
- Before pushing all the objects interested in the subject need to register.
- Observable (sends data to observers)
- Observers (subscribe)
Poll
- Object B checks every time if Object A has new data
-
The observer pattern defines a 1..* dependency between objects. When one object changes state all of its dependencies gets notified and updated automatically
UML
IObservable
- Add(IObserver o)
- Remove(IObserver o)
- Notify()
(IObservable) has 0* --> #
-
-