Observer Design Pattern — Mermaid Class Diagram
Class diagram showing the Observer pattern with EventEmitter, subscribers, and typed event handling.
What this diagram shows
This Mermaid class diagram is the Observer pattern applied to notifications. EventEmitter stores a map of event names to callbacks and exposes on, off, emit, and once. NotificationService extends EventEmitter, holds a list of NotificationChannel objects, and notifies them with an AppEvent. NotificationChannel is an interface with handle and supports. EmailChannel, SlackChannel, and WebhookChannel implement it. AppEvent carries a type, a payload, and a timestamp.
Use it when you are teaching the pattern with a concrete product example instead of the textbook Subject and Observer names. The inheritance arrow from EventEmitter to NotificationService is the part worth arguing about: a service that is an emitter mixes the registry with the domain. Composition (the service holds an emitter) is the variation if you do not want that.
Aggregation links the service to channels. The dashed arrow to AppEvent means the service uses the event type but does not own its lifecycle.
Mermaid syntax in this template
classDiagram relationship arrows are the pattern. <|-- is inheritance. <|.. is realization of an interface. o-- is aggregation. ..> is a dependency.
EventEmitter <|-- NotificationServiceThe triangle points at the parent. NotificationService is the subclass.
NotificationChannel <|.. EmailChannelDotted realization. The interface is marked <<interface>> inside the class block.
NotificationService o-- NotificationChannelAggregation: the service has channels and the channels can outlive one notification.
+on(event: string, callback: Function) voidPlus is public. Minus on listeners and removeAllListeners is private. The return type sits at the end of the method line.
Variations and ways to customize it
Prefer composition over extending EventEmitter
If the service should not be an emitter, drop the inheritance and give it a private emitter field. Channels stay behind the interface.
class NotificationService {
-EventEmitter bus
+subscribe(channel: NotificationChannel) void
}
NotificationService *-- EventEmitterFilter events inside supports
supports(eventType) is already on the interface. Show a call in a note or a method on the service that skips channels whose supports returns false, so emit does not fan out blindly.
Add a push channel
Copy the EmailChannel block, name it PushChannel, and add the realization arrow. Give it a device token field so it is not a rename of SlackChannel.
class PushChannel {
-deviceToken: string
+handle(event: AppEvent) void
}
NotificationChannel <|.. PushChannelDiagram type
Class DiagramTags
Frequently asked questions
Which class is the subject in this Observer diagram?
EventEmitter is the subject: it keeps listeners and emit. NotificationService is a subject too because it extends EventEmitter and also keeps channels. The channels are the observers.
What does the dotted arrow to AppEvent mean?
NotificationService ..> AppEvent is a dependency. The service uses the event in notify and does not contain it. Aggregation would be o-- or *-- if the service owned the event instances.
How do I mark a method as private?
Prefix the member with a minus, as listeners and removeAllListeners already do. Public members use a plus. Protected uses a hash. Mermaid reads the prefix; it is not decoration.
Should channels be an interface or an abstract class?
The template uses <<interface>> and a dotted realization arrow. Switch to <<abstract>> and a solid inheritance arrow if the base class should hold shared code, such as a retry wrapper around handle.
Related templates and guides
All Database Design templatesRole-Based Access Control (RBAC)
Class diagram for an RBAC system with users, roles, permissions, and resource-level access policies.
SaaS Multi-Tenant Schema
ER diagram for a multi-tenant SaaS application with organizations, members, roles, and subscription billing.
CMS Content Model
ER diagram for a headless CMS with pages, blocks, media, and versioned content publishing workflow.
Mermaid class diagram syntax
Inheritance, interface realization, and visibility markers used in this Observer model.
Getting started with Mermaid
How a classDiagram header differs from an ER diagram of the same domain.