搜索

PythonReactSEOPDF
GitHub Skill

design-pattern-observer

Use when state changes in one object must notify many dependents while keeping publishers decoupled from subscriber implementations.

zenobi-us☆ 39 仓库 Stars7 仓库 Forks

安装与使用

请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。

查看来源 ↗使用指南

技能说明

Skill: Observer

Intent

Define a subscription mechanism where subjects publish change notifications to registered observers. This supports one-to-many event propagation with low coupling between producers and consumers.

Applicability Signals

  • Signal 1: Multiple downstream reactions must occur when a source state changes.
  • Signal 2: Producer code should not hard-code knowledge of all consumers.
  • Signal 3: Consumers should be attachable/detachable at runtime.

Contraindications

  • Case 1: Notification targets are fixed and small, with no anticipated variation.
  • Case 2: Delivery guarantees/ordering requirements demand a formal messaging platform.
  • Case 3: Event storms are likely and backpressure/throttling architecture is absent.

Decision Heuristics

  • If producer/consumer coupling is the main pain and dynamic subscriptions matter, prefer Observer.
  • If interactions require central orchestration and rules, Mediator may fit better.
  • Decision anti-bias note: avoid emitting broad ambiguous events that force observers to guess context.

Implementation Checklist

  • Define subject and observer contracts with clear event payload shape.
  • Implement subscribe/unsubscribe lifecycle controls.
  • Keep subject focused on event emission, not consumer policy.
  • Handle observer failures (isolation, retry, or dead-letter policy as needed).
  • Test registration lifecycle and multi-observer notification behavior.

Misuse Checks

  • Misuse 1: Observers mutate subject recursively causing loops → Remediation: guard re-entrant updates and separate command paths.
  • Misuse 2: Subject event payloads are under-specified → Remediation: version and document explicit event contracts.
  • Misuse 3: Unsubscribed observers continue receiving events due to leaks → Remediation: verify unsubscribe semantics and lifecycle ownership.

Verification Rubric

  • Correctness:
    • All subscribed observers receive expected notifications for baseline change.
    • Unsubscribed observers stop receiving notifications.
  • Design quality:
    • Subject has no hard dependency on concrete observers.
    • Event contracts are explicit and stable.
  • Regression safety:
    • Tests cover multi-observer fanout and failure isolation path.

Language-Specific Adaptations (Optional)

  • TypeScript: use typed event maps to ensure payload correctness per event key.
  • Python: combine observer lists with weak references where lifecycle leaks are a risk.
  • Go: use channels/callback registries with explicit cancellation context handling.

Related Patterns (Optional)

  • Mediator: mediator centralizes decision-making; observer broadcasts state changes.
  • Command: observers can enqueue commands rather than executing side effects inline.

Attribution & Sources

使用示例

查看来源仓库中的使用示例与配置说明。

查看来源 ↗

来源历史

收录时间
2026-03-11
索引更新时间
2026-03-11
Branch
main

收录与同步日期属于本站记录。源码修改历史请查看来源仓库。

GitHub ↗

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!