1. this pattern defines a one-to-many dependency between objects so that when one object
changes state, all its dependents are notified and updated automatically
2.
3. for example, we have the weather station which consist of a weather data class that can read weather information like the humidity, the temperature, and the pressure. And display classes that display this weather data in various configurations, like the current conditions, the forecast. Each time the weather data object gets new weather data, it needs to update each of the displays. So the weather data class is our subject and the displays are the observers. So, the dependens between classes is the next - when weather data class get new data it should let them know when there is new data display
4. we have interface Subject; class WeatherData which implement this interface; interface Observer with one method update. Each of the displays implements this observer interface, and we have added another interface display element which all the display classes implement as well. This has just one method display. Each of the display classes has a reference to the weather data subject, so they can register themselves as observers. And our subject, weather data, keeps a list of all the observers it has, so it can notify the observers when it gets new weather information.
5. Некоторые аукционы демонстрируют этот паттерн. Каждый участник имеет карточку с цифрами, которую он использует для обозначения предлагаемой цены (ставки). Ведущий аукциона (Subject) начинает торги и наблюдает, когда кто-нибудь поднимает карточку, предлагая новую более высокую цену. Ведущий принимает заявку, о чем тут же извещает всех участников аукциона (Observers).




Немає коментарів:
Дописати коментар