0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【デザインパターン】 Mediator パターン vs Observer パターン

Posted at

共通点

  • どちらも オブジェクト間の依存関係を緩和する ためのデザインパターン
  • イベントや通知を介してコンポーネントを疎結合にする

違い(表で整理)

項目 Mediator Observer
目的 多対多の相互作用を 仲介者に一元化 1対多の通知を イベント購読モデルで実現
関係性 コンポーネントは Mediator だけを知る Observer は Subject に登録され、Subject → Observer に通知
依存方向 双方向(Mediator ↔ Colleague) 一方向(Subject → Observer)
ユースケース UI コンポーネント間の調停(ボタン → 入力欄 → エラー表示) 状態変化の通知(データ変更 → UI 更新)
メリット 相互依存を一箇所にまとめて管理できる 1対多の通知を疎結合に簡単に表現できる
リスク Mediator が「神オブジェクト化」しやすい Observer が増えすぎると把握が難しい

図解イメージ


まとめ(比喩で理解)

  • Observer = 「先生(Subject)が生徒(Observer)に連絡網で一斉通知する」
  • Mediator = 「クラス委員長(Mediator)が先生と生徒の間で連絡を仲介する」
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?