Observer pattern cho phép các object (observer) đăng ký nhận thông báo về thay đổi trạng thái của object khác (subject).
- Trong Android, LiveData/StateFlow là các implementation của pattern này.
- Khi bạn gọi
observe()hoặccollect(), bạn đang đăng ký làm observer. - Khi dữ liệu thay đổi, tất cả observer được thông báo, decoupling nguồn dữ liệu khỏi UI.
Observer pattern lets objects (observers) subscribe to state changes of another object (subject).
- In Android, LiveData/StateFlow are implementations.
- When you call
observe()orcollect(), you're registering as an observer. - When data changes, all observers are notified.
- This pattern decouples the data source from UI, allowing multiple UIs to react to the same data.