# Observer Pattern

> A design pattern in which a subject publishes events to subscribers it does not know by name.

Record: `architecture:observer-pattern`
Kind: pattern
Layer: [Design Patterns Core](https://banes-lab.com/records/layer/design-patterns-core.md)
Severity: recommended
Scope: event notification, runtime
Canonical: https://banes-lab.com/ontology#architecture-observer-pattern

Listed in [Architecture principles](https://banes-lab.com/api/records/architecture.md), after [Template Method Pattern](https://banes-lab.com/records/architecture/template-method-pattern.md) and before [Mediator Pattern](https://banes-lab.com/records/architecture/mediator-pattern.md).

## Repair

- Refactored by: Introduce Observer/Event Publisher
- Detected by: direct calls to multiple listeners
- Violated by: hardcoded notification targets
- Measured by: subscriber coupling count
- Enforced by: event contract tests

## Requires

- [Subject/Subscriber Contract](https://banes-lab.com/records/lexicon/subject-subscriber-contract.md)

## Reinforces

- [Event-Driven Architecture](https://banes-lab.com/records/architecture/event-driven-architecture.md)

## Enables

- [Decoupled Notification](https://banes-lab.com/records/lexicon/decoupled-notification.md)

## Conflicts with

- [Direct Callback Coupling](https://banes-lab.com/records/lexicon/direct-callback-coupling.md)

## In tension with

- [Ordering](https://banes-lab.com/records/lexicon/ordering.md)
- [Debuggability](https://banes-lab.com/records/lexicon/debuggability.md)

## Tensions

- [Observer Pattern / Ordering](https://banes-lab.com/records/tension/observer-pattern-ordering.md)
- [Observer Pattern / Debuggability](https://banes-lab.com/records/tension/debuggability-observer-pattern.md)

## Severity

- [recommended](https://banes-lab.com/records/vocabulary/severity-recommended.md)

## Category

- [Behavioral Patterns](https://banes-lab.com/records/architecture-category/behavioral-patterns.md)

## Linked from

- [Execution joins the halves](https://banes-lab.com/software-architecture/principles/execution-joins-the-halves.md)
- [Event / Messaging / Asynchronous Architecture](https://banes-lab.com/ontology/principles/architecture-category-event-messaging-asynchronous-architecture.md)
- [Behavioral Patterns](https://banes-lab.com/ontology/lexicon/lexicon-category-behavioral-patterns.md)
- [Quality Attributes](https://banes-lab.com/ontology/lexicon/lexicon-category-quality-attributes.md)
- [Severity levels](https://banes-lab.com/ontology/schema/the-vocabulary-severity.md)
- [The resolutions](https://banes-lab.com/ontology/schema/the-resolutions.md)

## Evidence in the code

- [emitEvent](https://banes-lab.com/source/tree/core/buses/base.bus.ts.md)
- [subscribeEvent](https://banes-lab.com/source/tree/core/buses/base.bus.ts.md)
- [core/buses](https://banes-lab.com/anatomy/tree/folder-core-buses.md)
