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!");
}

上一篇:canvas_12 跟随鼠标的小球


下一篇:方法中的数组类型的参数