7 May 2019, 08:24

the mediator pattern


Name: Mediator

Type: Behavioural

Use when you want to:

Mediate two objects (colleagues) and how they communicate without the colleagues knowing about eachothers implementations.

From Wikipedia:

In software engineering, the mediator pattern defines an object that encapsulates how a set of objects interact. This pattern is considered to be a behavioral pattern due to the way it can alter the program's running behavior.

Real World Example:

A general example would be when you talk to someone on your mobile phone, there is a network provider sitting between you and them and your conversation goes through it instead of being directly sent. In this case network provider is mediator.

Typescript Code Example:

Set up the mediator:

interface IChatRoom { 
    showMessage(user: User, message: string)
}

// Mediator
class ChatRoom implements IChatRoom {
    showMessage(user: User, message: string) {
        const time = new Date()
        const sender = user.getName()

        console.log(`${time}[${sender}]:${message}`)
    }
}

The colleagues:

class User {
    constructor(private name: string, private chatMediator: IChatRoom) {
    }
    
    getName() {
        return this.name
    }
    
    send(message) {
        this.chatMediator.showMessage(this, message)
    }
}

const mediator = new ChatRoom()

const john = new User('John Doe', mediator)
const jane = new User('Jane Doe', mediator)

john.send('Hi there!')
jane.send('Hey!')

← The Memento Pattern
The Iterator Pattern →