Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Notification Engine

A TypeScript-based notification system demonstrating multiple design patterns including Decorator, Observer, Strategy, and Singleton patterns.

Overview

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.

Design Patterns Implemented

1. Decorator Pattern

Allows dynamic addition of behavior to notifications without modifying the original notification class.

Components:

  • INotification - Base notification interface
  • SimpleNotification - Basic notification implementation
  • NotificationDecorator - Abstract decorator class
  • TimeStampDecorator - Adds timestamp to notifications
  • SignatureDecorator - 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'
);

2. Observer Pattern

Enables observers to subscribe and get notified when new notifications are sent.

Components:

  • IObserver - Observer interface with update() method
  • IObservable - Observable interface for managing observers
  • NotificationObservable - Manages notification state and observer list
  • Logger - Example observer that logs notifications
  • NotificationEngine - Observer that sends notifications via multiple strategies

Key Methods:

  • addObserver(observer) - Subscribe an observer
  • removeObserver(observer) - Unsubscribe an observer
  • notifyObserver() - Notify all observers of changes

3. Strategy Pattern

Allows switching between different notification delivery methods at runtime.

Components:

  • NotificatioNStrategy - Strategy interface
  • EmailStrategy - Sends notifications via email
  • SMSStrategy - Sends notifications via SMS
  • POPUpStrategy - Displays pop-up notifications

Example:

notificationEngine.addNotificationStrategy(new EmailStrategy('test@gmail.com'));
notificationEngine.addNotificationStrategy(new SMSStrategy(123456578));
notificationEngine.addNotificationStrategy(new POPUpStrategy());

4. Singleton Pattern

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();

Architecture

┌─────────────────────────────────────────────────────┐
│           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 │
              └──────────┘  └──────────┘  └──────────┘

How It Works

  1. Initialization:

    • NotificationService is created as a singleton
    • Observers (Logger, NotificationEngine) subscribe to NotificationObservable
    • Notification strategies are added to NotificationEngine
  2. Creating Notifications:

    • Base notification is created using SimpleNotification
    • Decorators are applied to add timestamp, signature, etc.
  3. Sending Notifications:

    • NotificationService.sendNotification() is called with the decorated notification
    • NotificationObservable updates its state and notifies all observers
    • Each observer's update() method is called
    • NotificationEngine sends the notification through all registered strategies

Usage Example

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();

Output

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

Key Features

  • 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

Running the Project

# 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

Future Enhancements

  • 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

Author

sulaiman@gmail.com

About

TypeScript notification system demonstrating Decorator, Observer, Strategy, and Singleton design patterns

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages