To use the Mediator pattern in a NestJS project, you can follow these steps:
Step 1: Install Required Dependencies
First, make sure you have NestJS installed in your project. Additionally, you may need to install any additional packages for implementing the Mediator pattern, such as a messaging library like RabbitMQ or a package specifically designed for Mediator pattern support.
Step 2: Create Mediator Class
Create a Mediator class that will handle communication between different components. This class will act as the central hub for handling requests and coordinating actions. It should have methods to register and execute commands or events.
// mediator.ts
import { Injectable } from '@nestjs/common';
@Injectable()
export class Mediator {
private handlers: Record<string, Function[]> = {};
register(event: string, handler: Function) {
if (!this.handlers[event]) {
this.handlers[event] = [];
}
this.handlers[event].push(handler);
}
execute(event: string, ...args: any[]) {
if (!this.handlers[event]) {
return;
}
for (const handler of this.handlers[event]) {
handler(...args);
}
}
}
Step 3: Register Handlers
In your modules or services, register handlers with the Mediator using the `register` method. Handlers can be functions or methods that will be executed when a specific event occurs.
// example.module.ts
import { Module } from '@nestjs/common';
import { Mediator } from './mediator';
import { ExampleService } from './example.service';
@Module({
providers: [Mediator, ExampleService],
exports: [Mediator],
})
export class ExampleModule {
constructor(private mediator: Mediator, private exampleService: ExampleService) {
this.mediator.register('exampleEvent', this.exampleService.handleEvent.bind(this.exampleService));
}
}
Step 4: Trigger Events
When you want to trigger an event, call the `execute` method on the Mediator class, passing the event name and any required arguments.
// example.service.ts
import { Injectable } from '@nestjs/common';
import { Mediator } from './mediator';
@Injectable()
export class ExampleService {
constructor(private mediator: Mediator) {}
triggerEvent() {
// Perform some logic
// ...
// Trigger event
this.mediator.execute('exampleEvent', arg1, arg2, ...);
}
handleEvent(arg1: any, arg2: any, ...) {
// Handle the event
// ...
}
}
Step 5: Use Mediator in Consumers
You can also use the Mediator to communicate between different components, such as between modules or services. Simply inject the Mediator instance into the consuming components and use it to register and execute events.
// consumer.service.ts
import { Injectable } from '@nestjs/common';
import { Mediator } from './mediator';
@Injectable()
export class ConsumerService {
constructor(private mediator: Mediator) {
this.mediator.register('exampleEvent', this.handleEvent.bind(this));
}
handleEvent(arg1: any, arg2: any, ...) {
// Handle the event in the consumer service
// ...
}
}
That's it! You have implemented the Mediator pattern in your NestJS project. Now, when an event is triggered using the Mediator, all the registered handlers will be executed and perform their respective actions.