• Home
  • Tutorials
  • Shop
  • AI News
  • Services
  • JaggoRe AITry
Get Started
  • Login
  • Register
Neuraldemy
Cart / 0.00$

No products in the cart.

No Result
View All Result
Get Started
Neuraldemy
Get started
Home Javascript

Factory Pattern In JavaScript

Neuraldemy by Neuraldemy
March 3, 2026
in Javascript
Reading Time: 2 mins read
A A

A dynamic notification service (Email, SMS, Push) built to strictly adhere to SOLID principles. By using the Factory pattern and interfaces, the system avoids fragile if/else blocks and remains fully open for extension but closed for modification.

// factory pattern
interface INotification {
  send(useId: string, message: string): void;
}

class EmailNotiification implements INotification {
  public send(userId: string, message: string): void {
    console.log(`[EMAIL] Sending to user ${userId}: ${message}`);
  }
}

class SMSNotiification implements INotification {
  public send(userId: string, message: string): void {
    console.log(`[SMS] Sending to user ${userId}: ${message}`);
  }
}

class PushNotification implements INotification {
  public send(userId: string, message: string): void {
    console.log(`[PUSSH] Sending to device for user ${userId}: ${message}`);
  }
}

enum NotificationType {
  EMAIL = "EMAIL",
  SMS = "SMS",
  PUSH = "PUSH",
}

class NotificationFactory {
  public static createNotification(type: NotificationType): INotification {
    switch (type) {
      case NotificationType.EMAIL:
        return new EmailNotiification();
      case NotificationType.SMS:
        return new SMSNotiification();
      case NotificationType.PUSH:
        return new PushNotification();
      default:
        throw new Error("Invalid notification type requested");
    }
  }
}

class NotificationService {
  public notifyUser(
    type: NotificationType,
    useId: string,
    message: string,
  ): void {
    const notifier = NotificationFactory.createNotification(type);
    notifier.send(useId, message);
  }
}

const myAppService = new NotificationService();

console.log("--- System Alerts ---");
myAppService.notifyUser(
  NotificationType.EMAIL,
  "user_101",
  "Welcome to the platform!",
);
myAppService.notifyUser(
  NotificationType.SMS,
  "user_102",
  "URGENT: Server CPU is at 99%.",
);
myAppService.notifyUser(
  NotificationType.PUSH,
  "user_103",
  "Your data export is ready.",
);Code language: JavaScript (javascript)
Previous Post

Inside OpenAI Military Deal and the Ousting of Anthropic

Next Post

Strategy Pattern In JavaScript

Neuraldemy

Neuraldemy

This is Neuraldemy support. Subscribe to our YouTube channel for more.

Related Posts

Strategy Pattern In JavaScript

Inside OpenAI Military Deal and the Ousting of Anthropic

Nano Banana 2: Google’s New AI Image Generator Just Fixed the Speed vs. Quality Problem

The 5 Best Books for Learning Machine Learning Mathematics

Singleton pattern In JavaScript

Dynamic Array Implementation Modern C++

Next Post

Strategy Pattern In JavaScript

  • Customer Support
  • Get Started
  • Ask Your ML Queries
  • Contact
  • Privacy Policy
  • Terms Of Use
Neuraldemy

© 2024 - A learning platform by Odist Magazine

Welcome Back!

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Fill the forms below to register

*By registering into our website, you agree to the Terms & Conditions and Privacy Policy.
All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In
No Result
View All Result
  • Home
  • Tutorials
  • Shop
  • AI News
  • Services
  • JaggoRe AI
  • Login
  • Sign Up
  • Cart
Order Details

© 2024 - A learning platform by Odist Magazine

This website uses cookies. By continuing to use this website you are giving consent to cookies being used.
0