Observer
📎 Observer pattern is used when there is one-to-many relationship between objects such as if one object's state is modified, its depenedent objects are to be notified automatically.
The same principal as the" Publish-Subscribe"
Diagram​
Example : Weather station (we should be notifed everytime the weather change)​
Our job is to create an app that uses the WeatherData object (which takes dtaa from a sensor) to update three displays elements
- Current conditions (temperature/humidity/pressure)
- Weather statistics
- forecast
these three are our observer concrete classes
so the weather data class should notify all the observers each time it has a new state
notify method in weatherData calls the update method in observers classes
the concrete observer is add as w subscriber to the subject = register to a subject