编程语言
首页 > 编程语言> > C++设计模式——12中介者模式

C++设计模式——12中介者模式

作者:互联网

#include<iostream>
#include<string>
using namespace std;

class User
{
    private:
        string name;
    public:
        string getName()
        {
            return name;
        }
        void setName(string name)
        {
            this->name=name;
        }
        User(string name)
        {
            this->name = name;
        }
};

class ChatRoom
{
    public:
        void showMessage(User *user, string message)
        {
            cout<<"["<<user->getName()<<"] : "<<message<<endl;
        }
};

int main()
{
    User *robert = new User("Robert");
    User *john = new User("John");
    ChatRoom chatRoom;
    chatRoom.showMessage(robert,"Hi! John!");
    chatRoom.showMessage(john,"Hello! Robert!");
}

标签:12,string,john,C++,User,chatRoom,showMessage,设计模式,name
来源: https://blog.csdn.net/qq_52553095/article/details/120479227