Observer pattern กำหนด dependency แบบหนึ่งต่อหลาย ที่อ็อบเจ็กต์หลายตัว (observer) ได้รับการแจ้งเตือนโดยอัตโนมัติเมื่ออ็อบเจ็กต์อีกตัว (subject) เปลี่ยนสถานะ มันช่วยให้เกิดการสื่อสารแบบ event-driven ที่ลด coupling ซึ่งถูกใช้อย่างกว้างขวางใน UI, ระบบ event และ reactive programming
Observer pattern ทำอะไร
OBSERVER → a SUBJECT maintains a list of OBSERVERS and NOTIFIES them on state changes:
→ observers SUBSCRIBE to the subject
→ when the subject changes → it notifies ALL subscribed observers automatically
→ observers react to the notification (update themselves)
→ one-to-many: one subject, many observers; DECOUPLED (subject doesn't know observer details)
