Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- interface EventListener {
- handleEvent(eventName: string, data: any): void;
- }
- class LoggerListener implements EventListener {
- handleEvent(eventName: string, data: any): void {
- console.log(`Logger: Event "${eventName}" occurred with data:`, data);
- }
- }
- class AnalyticsListener implements EventListener {
- handleEvent(eventName: string, data: any): void {
- console.log(`Analytics: Captured event "${eventName}" with data:`, data);
- }
- }
- class EventManager {
- private listeners: EventListener[] = [];
- addListener(listener: EventListener): void {
- this.listeners.push(listener);
- }
- triggerEvent(eventName: string, data: any): void {
- this.listeners.forEach((listener) => listener.handleEvent(eventName, data));
- }
- }
- // Пример использования
- const eventManager = new EventManager();
- eventManager.addListener(new LoggerListener());
- eventManager.addListener(new AnalyticsListener());
- eventManager.triggerEvent("user_login", { userId: 123 });
- // Logger: Event "user_login" occurred with data: { userId: 123 }
- // Analytics: Captured event "user_login" with data: { userId: 123 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement