This project demonstrates the implementation of the Facade design pattern in a notification system using TypeScript and Node.js.
The Facade pattern provides a simplified interface to a complex subsystem. It doesn't encapsulate the subsystem but provides a unified interface to a set of interfaces in a subsystem. This pattern defines a higher-level interface that makes the subsystem easier to use.
In this project, we use the Facade pattern to simplify the process of sending different types of notifications (email, SMS, push) through a single interface.
notification-system-facade/
├── src/
│ ├── services/
│ │ ├── EmailService.ts
│ │ ├── SMSService.ts
│ │ └── PushNotificationService.ts
│ ├── NotificationFacade.ts
│ └── main.ts
├── package.json
├── tsconfig.json
└── README.md
- Node.js (v12 or later)
- npm (usually comes with Node.js)
-
Clone the repository:
git clone https://github.com/simonedimeglio/notification-system-facade.git cd notification-system-facade
-
Install dependencies:
npm install
To compile and run the project during development:
npm run dev
This command will compile the TypeScript files and then run the main script.
To compile the TypeScript files without running the project:
npm run build
This will create compiled JavaScript files in the dist/
directory.
To run the compiled JavaScript:
npm start
This command runs the compiled main.js
file from the dist/
directory.
-
We have three separate services:
EmailService
,SMSService
, andPushNotificationService
, each responsible for sending a specific type of notification. -
The
NotificationFacade
class provides a simplified interface to these services. It encapsulates the complexity of choosing which service to use and how to use it. -
Clients of the notification system only need to interact with the
NotificationFacade
, which provides methods for sending individual types of notifications as well as a method to send all types at once.
import { NotificationFacade } from "./NotificationFacade";
const notifier = new NotificationFacade();
// Send individual notifications
notifier.sendEmail("[email protected]", "Welcome", "Welcome to our service!");
notifier.sendSMS("+1234567890", "Your OTP is 123456");
notifier.sendPushNotification("user123", "You have a new message");
// Send all types of notifications at once
notifier.sendAll(
"[email protected]",
"+1234567890",
"user123",
"Important Update",
"This is an important message sent via all channels"
);
- Simplification: Clients don't need to know about the complexities of the underlying notification services.
- Decoupling: The facade decouples the client code from the subsystem's components.
- Single Point of Entry: Provides a single entry point for sending any type of notification.
- Flexibility: New notification types can be added to the subsystem without changing the facade's interface.
To add a new type of notification:
- Create a new service in the
services/
directory. - Add the new service to the
NotificationFacade
class. - Add a new method in
NotificationFacade
to handle the new notification type. - Update the
sendAll
method inNotificationFacade
to include the new notification type.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the project
- Create your feature branch (
git checkout -b feature/AmazingFeature
) - Commit your changes (
git commit -m 'Add some AmazingFeature'
) - Push to the branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- This project was created to demonstrate the Facade design pattern in a practical scenario.
- Thanks to the TypeScript and Node.js communities for providing excellent tools and documentation.