← Back to scenarios
ScenarioCatalog DB

Stock price alerts

Notify dashboards and alert services whenever a tracked stock price changes.

Problem

A market-data system must notify multiple independent consumers of stock-price changes without coupling the price source to concrete dashboard or alert implementations.

Related patterns

Implementations

Observer

TypeScriptApplication

Observer in TypeScript

Publishes deployment-status changes to independently subscribed notification channels.

TypeScript
interface DeploymentObserver {
  update(status: string): void;
}

class Deployment {
  private readonly observers = new Set<DeploymentObserver>();
  private status = "pending";

  subscribe(observer: DeploymentObserver) {
    this.observers.add(observer);
  }

  unsubscribe(observer: DeploymentObserver) {
    this.observers.delete(observer);
  }

  setStatus(status: string) {
    this.status = status;
    this.notify();
  }

  private notify() {
    for (const observer of this.observers) {
      observer.update(this.status);
    }
  }
}

class SlackNotifier implements DeploymentObserver {
  update(status: string) {
    console.log(`Slack: deployment is ${status}`);
  }
}

class DashboardNotifier implements DeploymentObserver {
  update(status: string) {
    console.log(`Dashboard updated: ${status}`);
  }
}

const deployment = new Deployment();

deployment.subscribe(new SlackNotifier());
deployment.subscribe(new DashboardNotifier());

deployment.setStatus("running");
deployment.setStatus("completed");

Associated technologies