Tuesday, January 30, 2024

C++ Mediator Design Pattern using C++

The Mediator design pattern is used to centralize complex communications and control between related objects, making it easier to decouple them. Here's a simple example using a chat room (Mediator) where users (Colleagues) can send messages to each other:

1. 'Mediator.h`
This is an abstract class that declares the `sendMessage` method.


#include <string>

class User;

class Mediator {
public:
    virtual void sendMessage(const std::string& message, User* user) = 0;
};

2. `User.h`
This represents a colleague class. Each user knows about the mediator and can send messages.
#include "Mediator.h"
#include <iostream>

class User {
protected:
    Mediator* _mediator;
    std::string _name;

public:
    User(Mediator* mediator, const std::string& name) : _mediator(mediator), _name(name) {}
    virtual ~User() {}

    void sendMessage(const std::string& message) {
        _mediator->sendMessage(message, this);
    }

    virtual void receiveMessage(const std::string&smp; message) {
        std::cout << _name << " received: " << message << std::endl;
    }

    const std::string& getName() const { return _name; }
};

3. `ChatRoom.h`
This concrete mediator allows users to send messages to each other.
#include "Mediator.h"
#include "User.h"
#include <vector>

class ChatRoom : public Mediator {
private:
    std::vector<User*> _users;

public:
    void addUser(User* user) {
        _users.push_back(user);
    }

    void sendMessage(const std::string& message, User* user) override {
        for (User* u : _users) {
            // Don't send the message back to the sender
            if (u != user) {
                u->receiveMessage(user->getName() + ": " + message);
            }
        }
    }
};

4. `main.cpp`
This is a simple demo using the classes.
cpp
#include "ChatRoom.h"

int main() {
    ChatRoom chatRoom;

    User* alice = new User(&chatRoom, "Alice");
    User* bob = new User(&chatRoom, "Bob");

    chatRoom.addUser(alice);
    chatRoom.addUser(bob);

    alice->sendMessage("Hi Bob!");
    bob->sendMessage("Hello Alice!");

    delete alice;
    delete bob;

    return 0;
}
When you run this, you will get:
Bob received: Alice: Hi Bob!
Alice received: Bob: Hello Alice!

Explanation:

1. Mediator: Abstract class to define the contract for concrete mediators.
2. User: Represents the colleagues that will communicate using the Mediator.
3. ChatRoom: Concrete mediator that allows `User` instances to communicate with each other.
4. main.cpp: Demonstrates the usage of the pattern.

The idea is that a `User` doesn't communicate with other users directly. Instead, they use the `ChatRoom` (mediator) to pass messages. The mediator then decides how to propagate that message, allowing for easy modification of behavior without changing the `User` classes.

No comments:

The Strategy Design Pattern a Behavioral Pattern using C++

The Strategy Design Pattern is a behavioral design pattern that enables selecting an algorithm's implementation at runtime. Instead of i...