C++ intermediary mode of design mode 17 (including sample code)

The Mediator pattern is a behavioral design pattern that decouples the interaction between objects by forwarding their communication to a mediator object. The intermediary pattern can be used to implement complex interaction logic, coordinate behaviors between multiple objects, and other applications.

The following is a sample code to implement the mediator pattern in C++:

#include <iostream>
#include <string>
#include <vector>
// 抽象中介者类
class Mediator {
    
    
public:
    virtual ~Mediator() {
    
    }
    virtual void Send(const std::string& message, const class Colleague* colleague) const = 0;
};
// 抽象同事类
class Colleague {
    
    
public:
    virtual ~Colleague() {
    
    }
    virtual void Receive(const std::string& message) const = 0;
    virtual void Send(const std::string& message) const = 0;
    void SetMediator(const Mediator* mediator) {
    
    
        mediator_ = mediator;
    }
protected:
    const Mediator* mediator_;
};
// 具体中介者类:聊天室
class ChatRoom : public Mediator {
    
    
public:
    void Send(const std::string& message, const Colleague* colleague) const override {
    
    
        std::cout << colleague->GetName() << " sends message: " << message << std::endl;
        for (const auto& c : colleagues_) {
    
    
            if (c != colleague) {
    
    
                c->Receive(message);
            }
        }
    }
    void Register(Colleague* colleague) {
    
    
        colleagues_.push_back(colleague);
    }
private:
    std::vector<Colleague*> colleagues_;
};
// 具体同事类:用户
class User : public Colleague {
    
    
public:
    User(const std::string& name) : name_(name) {
    
    }
    void Receive(const std::string& message) const override {
    
    
        std::cout << name_ << " receives message: " << message << std::endl;
    }
    void Send(const std::string& message) const override {
    
    
        mediator_->Send(message, this);
    }
    const std::string& GetName() const {
    
    
        return name_;
    }
private:
    std::string name_;
};
int main() {
    
    
    // 创建聊天室对象
    ChatRoom chat_room;
    // 创建用户对象
    User alice("Alice");
    User bob("Bob");
    User charlie("Charlie");
    // 注册用户对象到聊天室
    chat_room.Register(&alice);
    chat_room.Register(&bob);
    chat_room.Register(&charlie);
    // 设置用户对象的中介者为聊天室
    alice.SetMediator(&chat_room);
    bob.SetMediator(&chat_room);
    charlie.SetMediator(&chat_room);
    // 用户对象之间发送消息
    alice.Send("Hello, Bob and Charlie!");
    bob.Send("Hi, Alice!");
    charlie.Send("Nice to meet you all!");
    return 0;
}

In the above code, we first define a Mediator abstract class, which contains the Send () pure virtual function. The specific intermediary class ChatRoom implements the Send() function to forward messages to other colleagues. The abstract colleague class Colleague defines Receive() and Send() pure virtual functions for receiving and sending messages. The concrete colleague class User implements Receive() and Send() functions to interact with other users in the chat room.

In the main() function, we first created a chat room object chat_room, then created three user objects alice, bob and charlie, and registered them in the chat room. Next, we set the mediator of the user object as a chat room. Finally, we let the user objects send messages to each other and observe the output.

The advantage of the mediator pattern is that it transfers the interaction between objects to the mediator object, thereby reducing the coupling between objects. At the same time, the intermediary model also provides a centralized control method, making the system easier to maintain and expand. However, since the mediator object needs to handle a lot of logic, it can become a bottleneck in the system.

Guess you like

Origin blog.csdn.net/dica54dica/article/details/130021425