🎁 Get the FREE AI Skills Starter Guide β€” Subscribe β†’
BytesAgainBytesAgain
πŸ¦€ ClawHub

Observer Pattern Implementor

by @quochungto

Implement the Observer pattern to establish one-to-many dependencies where changing one object automatically notifies and updates all dependents. Use when a...

Versionv1.0.0
⚑ When to Use
TriggerAction
- An abstraction has two separable aspects β€” a subject (data) and observers (views/reactions) β€” and tightly coupling them limits reuse of each independently
- A change to one object requires notifying an unknown or variable number of other objects, and you don't want to hard-code who those are
- Objects should be able to notify others without making assumptions about who those objects are β€” you want zero coupling from subject to observer
Do not apply if:
- Only one consumer will ever exist and that is certain by design (direct call is simpler)
- Observers trigger further changes that trigger more observers β€” cascade control is complex; use ChangeManager (Step 9)
- The language/framework already provides this mechanism natively (e.g., React state, RxJS, Qt signals) β€” use those instead
Before starting, verify you can answer: What is the subject (the thing that changes)? What are the observers (the things that react)? What state do observers need from the subject after a change?
---
πŸ’‘ Examples

Example 1: Spreadsheet Model with Multiple Chart Views

Scenario: A spreadsheet has a data model that three chart widgets (bar chart, pie chart, line chart) read directly. Currently the model calls barChart.refresh(), pieChart.refresh(), and lineChart.refresh() in its SetCell method. Adding a fourth chart requires modifying the model.

Trigger: "Every new chart type requires touching the spreadsheet model."

Process:

  • Step 1: Audit reveals SpreadsheetModel directly references three concrete chart classes
  • Step 2: SpreadsheetModel becomes ConcreteSubject; each chart implements Observer
  • Step 3: List in Subject (few subjects, multiple observers)
  • Step 4: Automatic trigger β€” SetCell calls Notify() after updating data
  • Step 5: Notify is last in SetCell β€” state is fully updated first
  • Step 6: Pull model β€” charts call GetCellValue(row, col) in their Update
  • Step 8: No complex dependencies β€” skip ChangeManager
  • Output: SpreadsheetModel has no reference to any chart class. New charts attach themselves; model never changes.


    Example 2: Clock with Multiple Display Observers

    Scenario: A ClockTimer (concrete subject) notifies time displays. A DigitalClock and AnalogClock both observe the same timer. This is the canonical sample from the GoF text.

    Trigger: "Both clock displays must always show the same time, but neither should know about the other."

    Key implementation details:

  • DigitalClock inherits from both Widget (UI toolkit) and Observer (combining concern 10 β€” Subject/Observer in one class hierarchy via multiple inheritance)
  • Constructor calls _subject->Attach(this); destructor calls _subject->Detach(this) β€” full lifecycle management
  • Update guards on subject identity: if (theChangedSubject == _subject) β€” handles potential future case of observing multiple subjects
  • void DigitalClock::Update(Subject* theChangedSubject) {
        if (theChangedSubject == _subject) {
            Draw();
        }
    }

    void DigitalClock::Draw() { int hour = _subject->GetHour(); int minute = _subject->GetMinute(); // render digital display }

    Output: ClockTimer::Tick() calls Notify(); both displays update independently without knowing each other exist.


    Example 3: Multi-Subject Dashboard with ChangeManager

    Scenario: A financial dashboard has three data subjects: PriceSubject, VolumeSubject, and PositionSubject. A RiskSummaryObserver observes all three. When a trade executes, all three subjects change in one operation. Without ChangeManager, RiskSummaryObserver.Update fires three times for one logical event.

    Trigger: "Our risk view recalculates three times per trade because it observes three subjects."

    Process:

  • Step 8: Observer observes multiple subjects β€” DAG dependency. ChangeManager required.
  • Step 9: Implement DAGChangeManager. Each subject calls ChangeManager::Instance()->Notify(). DAGChangeManager marks RiskSummaryObserver when PriceSubject changes, marks it again when VolumeSubject changes (already marked β€” no-op), then updates it once.
  • Output: One trade β†’ three subject changes β†’ RiskSummaryObserver.Update called exactly once.


    View on ClawHub
    TERMINAL
    clawhub install bookforge-observer-pattern-implementor

    πŸ§ͺ Use this skill with your agent

    Most visitors already have an agent. Pick your environment, install or copy the workflow, then run the smoke-test prompt above.

    πŸ” Can't find the right skill?

    Search 60,000+ AI agent skills β€” free, no login needed.

    Search Skills β†’