← Back to PatternPip

OBSERVER / THE WALKTHROUGH

A reading changes.
Who needs to know?

See why subscriptions help when the audience keeps changing.

CHAPTER 1 OF 3

A reading has one place to go.

A weather station publishes a temperature. One display needs it, so the station calls that display directly.

Publish 18 °C and watch the display receive it.

LIVE LOCAL EXAMPLE

Your weather station

One publish, one direct call.

Whole numbers from −20 to 50. Try −4 for frost.

DIRECT RECIPIENTS1 connected

Temperature display

Direct call

No reading yet

0 received

No reading published yet.

Follow the last delivery

Publish a reading to see the calls.

These calls run synchronously. The list records real callback deliveries.

A direct connection is enough.

The station tells its one display about the reading. This is easy to follow while the audience is fixed.

Inspect the current publishing code
export class SingleDisplayStation {
  private display: WeatherObserver;
  constructor(display: WeatherObserver) {
    this.display = display;
  }
  publish(temperature: number): void {
    this.display.update(createReading(temperature));
  }
}

This is the publisher used by the live example. Callback instrumentation records each actual delivery.

Each chapter starts a fresh example. Theme and language keep your place within a chapter.