每日博客

中介者模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、理解中介者模式的动机,掌握该模式的结构;

2、能够利用中介者模式解决实际问题。

 

 

[实验任务一]:虚拟聊天室

在“虚拟聊天室”实例中增加一个新的具体聊天室类和一个新的具体会员类,要求如下:

1. 新的具体聊天室中发送的图片大小不得超过20M。

2. 新的具体聊天室中发送的文字长度不得超过100个字符。

3. 新的具体会员类可以发送图片信息和文本信息。

4. 新的具体会员类在发送文本信息时,可以在信息后加上时间。

修改客户端测试类,注意原有系统类代码和客户端代码的修改。

C++

#include<iostream>
#include<map>
#include<ctime>
#include<sstream>
using namespace std;
class AbstractChatroom;
string m_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 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;
}
};
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 = m_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 = m_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);
}
};
int main() {
AbstractChatroom *newChat = new NewChat();

stringstream ss;

Member *member1, *member2, *member3, *member4, *member5, *member6;
member1 = new DiamondMember("A");
member2 = new DiamondMember("B");
member3 = new CommonMember("C");
member4 = new CommonMember("D");
member5 = new CommonMember("E");

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


member1->sendText("B", "我是B");
member2->sendText("A", "我是A");
member1->sendText("A", "TEST");
member2->sendImage("A", "测试");
member2->sendImage("A", "A.jpg");
member3->sendText("C", "TEST");
member4->sendText("D", "TEST");
member5->sendText("E", "TEST");
member5->sendImage("E", "E.jpg");
for (int i = 0; i < 110; i++) {
ss << "-";
}
return 0;
}

posted @ 2021-11-02 19:02  谦寻  阅读(22)  评论(0编辑  收藏  举报