首页 > 其他分享 >中介者模式(二)

中介者模式(二)

时间:2022-12-23 20:45:35浏览次数:37  
标签:Member name void 模式 member 中介 message string

#include<iostream>
#include<map>
#include<ctime>
#include<sstream>
using namespace std;
class AbstractChatroom;
//抽象同事类
class Member {
protected :
    AbstractChatroom *chatroom;
    string name;
public:
    Member(string name) {
        this->name=name;
    }
    AbstractChatroom* getChatroom() {
        return chatroom;
    }
    void setChatroom(AbstractChatroom *chatroom) {
        this->chatroom = chatroom;
    }
    string getName() {
        return name;
    }
    void setName(string name) {
        this->name = name;
    }
    virtual void sendText(string to,string message)=0;
    virtual void sendImage(string to,string image)=0;
    void receiveText(string from,string message) {
        cout<<from<<"发送文本给"<<this->name<<",内容为:"<<message<<endl;
    }
    void receiveImage(string from,string image) {
        cout<<from<<"发送图片给"<<this->name<<",内容为:"<<image<<endl;
    }
};
string replace(string strSrc,const string &oldStr, const string &newStr,int count=-1)
{
    string strRet=strSrc;
    size_t pos = 0;
    int l_count=0;
    if(-1 == count) //替换所有
        count = strRet.size();
    while ((pos = strRet.find(oldStr, pos)) != string::npos)
    {
        strRet.replace(pos, oldStr.size(), newStr);
        if(++l_count >= count) break;
        pos += newStr.size();
    }
    return strRet;
}
//抽象中介者类
class AbstractChatroom {
public:
    virtual void registers(Member *member)=0;
    virtual void sendText(string from,string to,string message)=0;
    virtual void sendImage(string from,string to,string image)=0;
};
//具体中介者类
class ChatGroup: public AbstractChatroom{
private:
    map<string, Member*> members;
    map<string, Member*>::iterator iter;
public:
    void registers(Member *member) {
        iter=members.find(member->getName());
        if(iter==members.end()) {
            members.insert(map<string, Member*>::value_type(member->getName(),member));
            member->setChatroom(this);
        }
    }
    void sendText(string from, string to, string message) {
        Member *member=members.find(to)->second;
        string newMessage=message;
        newMessage=replace(message,"日", "*");
        member->receiveText(from, newMessage);
    }
    void sendImage(string from, string to, string image) {
        Member *member= members.find(to)->second;
        if(image.length()>5) {
           cout<<"图片太大,发送失败"<<endl;
        }
        else {
            member->receiveImage(from, image);
        }
    }
};
//具体中介者类
class NewChat:public AbstractChatroom{
private:
    map<string, Member*> members;
    map<string, Member*>::iterator iter;
public:
    void registers(Member *member) {
       iter=members.find(member->getName());
        if(iter==members.end()) {
            members.insert(map<string, Member*>::value_type(member->getName(),member));
            member->setChatroom(this);
        }
    }
    void sendText(string from, string to, string message) {
        Member *member=members.find(to)->second;
        string newMessage=message;
        newMessage=replace(message,"日", "*");
        if(message.length()>100) {
            cout<<"发送的文字长度不得超过100个字符,发送失败"<<endl;
        }
        else {
            member->receiveText(from, newMessage);
        }
    }
    void sendImage(string from, string to, string image) {
        Member *member=members.find(to)->second;
        if(image.length()>20) {
            cout<<"图片大小不得超过20M,发送失败"<<endl;
        }
        else {
            member->receiveImage(from, image);
        }
    }
};
//具体同事类
class CommonMember :public Member{
protected :
    string name;
public:
    CommonMember(string name):Member(name) {
        this->name=name;
    }
    void sendText(string to, string message) {
        cout<<"普通会员发送消息:"<<endl;
        chatroom->sendText(this->name, to, message);
    }

    void sendImage(string to, string image) {
        cout<<"普通会员不能发送图片!"<<endl;
    }
};
//具体同事类
class DiamondMember :public Member{
protected :
    string name;
public:
    DiamondMember(string name):Member(name){
        this->name=name;
        }
    void sendText(string to, string message) {
        cout<<"钻石会员发送消息:"<<endl;
        chatroom->sendText(name,to, message);
    }
    void sendImage(string to, string image) {
        cout<<"钻石会员发送图片:"<<endl;
        chatroom->sendImage(name, to, image);
    }
};
//具体同事类
class NewMember :public Member{
protected :
    string name;
public:
    NewMember(string name):Member(name){
        this->name=name;
    }
    void sendText(string to, string message) {
        time_t now = time(NULL);
        tm* t = localtime(&now);
        // 将信息输出到字符串流
        stringstream ss;
        stringstream ss2;
        ss << t->tm_year + 1900 << "." << t->tm_mon + 1 << "." <<
            t->tm_mday << ".." << t->tm_hour << "." << t->tm_min << "." << t->tm_sec;
        cout<<"新会员发送消息:"<<endl;
        chatroom->sendText(name,to,message+"  现在时间"+ss.str());
    }
    void sendImage(string to, string image) {
        cout<<"会员发送图片:"<<endl;
        chatroom->sendImage(name, to, image);
    }
};
//测试函数
int main(){
        AbstractChatroom *newChat=new NewChat();
        Member *member1,*member2,*member3,*member4;
        member1=new DiamondMember("adam");
        member2=new CommonMember("justin");
        member3=new NewMember("Lil ghost");
        member4=new NewMember("kun");

        newChat->registers(member1);
        newChat->registers(member2);
        newChat->registers(member3);
        newChat->registers(member4);

        member1->sendText("justin","hey");
        member2->sendText("adam","hello");
        member3->sendText("kun","-----------------------------------------------------------------------------------------------------");
        member3->sendImage("kun","happy.jpg");
        member4->sendText("Lil ghost","hi");
        member4->sendImage("Lil ghost","happy.jpg--------------------");
}

 

标签:Member,name,void,模式,member,中介,message,string
From: https://www.cnblogs.com/qiuyutingyyds/p/17001582.html

相关文章

  • rsync的三种传输模式及常见报错
    rsync的传输的三种模式:1,本地传输模式(类似于cp,不支持推送和拉取,只是单纯的复制)2,远程传输模式(类似于scp,又不同于scp),scp只支持全量备份,rsync支持增量备份和差异备份3......
  • 状态模式(二)
    #include<iostream>#include<string>usingnamespacestd;classAccount;classAccountState{public:Account*acc;doublebalance;stringstateName;p......
  • 状态模式(一)
    用Java代码模拟实现课堂上的“银行账户”的实例,要求编写客户端测试代码模拟用户存款和取款,注意账户对象状态和行为的变化。 //Account.javapackagetest22;publiccl......
  • 策略模式(二)
    #include<iostream>usingnamespacestd;classTravelStrategy{public:virtualvoidtravel()=0;};classAirplaneStrategy:publicTravelStrategy{public:......
  • 策略模式(一)
    旅游的出行方式有乘坐飞机旅行、乘火车旅行和自行车游,不同的旅游方式有不同的实现过程,客户可以根据自己的需要选择一种合适的旅行方式。 //Person.javapackagetest2......
  • Dubbo架构设计与源码解析(三)责任链模式
    作者:周可强一、责任链模式简介1、责任链模式定义责任链(ChainofResponsibility)模式的定义:为了避免请求发送者与多个请求处理者耦合在一起,于是将所有请求的处理者通过前一对......
  • PORT模式已成功,请更新您的站点配置文件
    如何在FlashFXP中设置连接FTP服务器的模式为PORT呢? FlashFXP提供了两种连接FTP服务器的方式被动模式(PASV)、主动模式(PORT),而FlashFXP的默认设置为被动模式(PASV),更改FlashFXP将......
  • 观察者模式javac++
    软件设计                 石家庄铁道大学信息学院 实验21:观察者模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解观察者模式......
  • 状态模式javac++
    软件设计                 石家庄铁道大学信息学院 实验 22:状态模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容: 1、理解状态模式的......
  • 策略模式javac++
    软件设计                 石家庄铁道大学信息学院 实验23:策略模式本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:1、理解策略模式的动......