オブザーバーパターンは、1対多の依存関係を定義するパターンで、別のオブジェクト(サブジェクト)が状態を変更すると、複数のオブジェクト(オブザーバー)が自動的に通知されます。これにより、疎結合なイベント駆動型の通信が可能になり、UI、イベントシステム、リアクティブプログラミングで広く使用されています。
オブザーバーパターンが行うこと
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)
例
{
observers = [];
() { ..(obs); }
() { ..( obs.(data)); }
}
subject.({ : .(, data) });
subject.();
