A TypeScript-based notification system demonstrating multiple design patterns including Decorator, Observer, Strategy, and Singleton patterns.
This notification engine provides a flexible and extensible way to create, decorate, and send notifications through multiple channels. It showcases how different design patterns can work together to create a robust system.
Allows dynamic addition of behavior to notifications without modifying the original notification class.
Components:
INotification- Base notification interfaceSimpleNotification- Basic notification implementationNotificationDecorator- Abstract decorator classTimeStampDecorator- Adds timestamp to notificationsSignatureDecorator- Adds signature to notifications
Example:
const notification = new SimpleNotification('Your Order has been placed');
const timestampNotification = new TimeStampDecorator(notification);
const signatureNotification = new SignatureDecorator(
timestampNotification,
'Trademart'
);Enables observers to subscribe and get notified when new notifications are sent.
Components:
IObserver- Observer interface withupdate()methodIObservable- Observable interface for managing observersNotificationObservable- Manages notification state and observer listLogger- Example observer that logs notificationsNotificationEngine- Observer that sends notifications via multiple strategies
Key Methods:
addObserver(observer)- Subscribe an observerremoveObserver(observer)- Unsubscribe an observernotifyObserver()- Notify all observers of changes
Allows switching between different notification delivery methods at runtime.
Components:
NotificatioNStrategy- Strategy interfaceEmailStrategy- Sends notifications via emailSMSStrategy- Sends notifications via SMSPOPUpStrategy- Displays pop-up notifications
Example:
notificationEngine.addNotificationStrategy(new EmailStrategy('test@gmail.com'));
notificationEngine.addNotificationStrategy(new SMSStrategy(123456578));
notificationEngine.addNotificationStrategy(new POPUpStrategy());Ensures only one instance of the notification service exists throughout the application.
Component:
NotificationService- Central service managing the notification system
Usage:
const notificationService = NotificationService.getInstance();┌─────────────────────────────────────────────────────┐
│ NotificationService (Singleton) │
│ - Manages NotificationObservable │
│ - Coordinates notification flow │
└─────────────────────┬───────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────┐
│ NotificationObservable (Subject) │
│ - Maintains observer list │
│ - Notifies observers on new notifications │
└─────────────────────┬───────────────────────────────┘
│
┌───────────┴───────────┐
▼ ▼
┌──────────────────┐ ┌──────────────────┐
│ Logger │ │ NotificationEngine│
│ (Observer) │ │ (Observer) │
│ │ │ │
│ - Logs events │ │ - Uses Strategy │
│ │ │ Pattern │
└──────────────────┘ └─────────┬─────────┘
│
┌─────────────┼─────────────┐
▼ ▼ ▼
┌──────────┐ ┌──────────┐ ┌──────────┐
│ Email │ │ SMS │ │ PopUp │
│ Strategy │ │ Strategy │ │ Strategy │
└──────────┘ └──────────┘ └──────────┘
-
Initialization:
NotificationServiceis created as a singleton- Observers (
Logger,NotificationEngine) subscribe toNotificationObservable - Notification strategies are added to
NotificationEngine
-
Creating Notifications:
- Base notification is created using
SimpleNotification - Decorators are applied to add timestamp, signature, etc.
- Base notification is created using
-
Sending Notifications:
NotificationService.sendNotification()is called with the decorated notificationNotificationObservableupdates its state and notifies all observers- Each observer's
update()method is called NotificationEnginesends the notification through all registered strategies
const main = () => {
// Get singleton instance
const notificationService = NotificationService.getInstance();
// Create observers
const logger = new Logger();
const notificationEngine = new NotificationEngine();
// Add notification strategies
notificationEngine.addNotificationStrategy(
new EmailStrategy('test@gmail.com')
);
notificationEngine.addNotificationStrategy(new SMSStrategy(123456578));
notificationEngine.addNotificationStrategy(new POPUpStrategy());
// Create and decorate notification
const notification = new SimpleNotification('Your Order has been placed');
const timestampNotification = new TimeStampDecorator(notification);
const signatureNotification = new SignatureDecorator(
timestampNotification,
'Trademart'
);
// Send notification (triggers all observers and strategies)
notificationService.sendNotification(signatureNotification);
};
main();When executed, the notification will be sent through all registered strategies:
sending email notification: Your Order has been placed Timestamp: 2025-11-06T... Singature: Trademart to: test@gmail.com
sending SMS Your Order has been placed Timestamp: 2025-11-06T... Singature: Trademart to mobile number: 123456578
Pop up notification: Your Order has been placed Timestamp: 2025-11-06T... Singature: Trademart
- Extensible: Easily add new decorators, observers, or notification strategies
- Decoupled: Components interact through interfaces, promoting loose coupling
- Flexible: Combine decorators and strategies in any configuration
- Centralized: Single notification service manages all notification flow
- Type-Safe: Full TypeScript type checking and interfaces
# Install dependencies (if any)
npm install
# Run with ts-node
npx ts-node src/notifcationEngine.ts
# Or compile and run
tsc src/notifcationEngine.ts
node src/notifcationEngine.js- Add more decorator types (priority, category, etc.)
- Implement additional notification strategies (Push, Slack, Discord)
- Add notification queuing and scheduling
- Implement notification history and persistence
- Add filtering and notification preferences
- Unit tests for all patterns