设计模式-中介者模式
虚拟聊天室
在“虚拟聊天室”实例中增加一个新的具体聊天室类和一个新的具体会员类,要求如下:
1. 新的具体聊天室中发送的图片大小不得超过20M。
2. 新的具体聊天室中发送的文字长度不得超过100个字符。
3. 新的具体会员类可以发送图片信息和文本信息。
4. 新的具体会员类在发送文本信息时,可以在信息后加上时间。
修改客户端测试类,注意原有系统类代码和客户端代码的修改。
源代码
Java
package rjsj.no19; public class DiamondMember extends Member{ public DiamondMember(String name) { super(name); } @Override public void sendText(String to, String message) { System.out.println("钻石会员发送信息:"); chatroom.sendText(name,to,message); //发送 } @Override public void sendImage(String to, String image) { System.out.println("钻石会员发送图片:"); chatroom.sendImage(name,to,image); //发送 } }
package rjsj.no19; public abstract class Member { protected AbsChatroom chatroom; protected String name; public Member(String name) { this.name = name; } public AbsChatroom getChatroom() { return chatroom; } public void setChatroom(AbsChatroom chatroom) { this.chatroom = chatroom; } public String getName() { return name; } public void setName(String name) { this.name = name; } public abstract void sendText(String to,String message); public abstract void sendImage(String to,String image); public void receiveText(String from,String message) { System.out.println(from + "发送文本给" + this.name + ",内容为:" + message); } public void receiveImage(String from,String image) { System.out.println(from + "发送图片给" + this.name + ",内容为:" + image); } }
package rjsj.no19; import java.text.SimpleDateFormat; import java.util.Date; public class NewMember extends Member{ public NewMember(String name) { super(name); } public static String getTime() {//获取日期 Date date = new Date(); SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss"); return "发送时间:"+formatter.format(date); } @Override public void sendText(String to, String message) { System.out.println("新会员发送信息:"); chatroom.sendText(name,to,message + '\t' +getTime()); //发送 } @Override public void sendImage(String to, String image) { System.out.println("新会员发送图片:"); chatroom.sendImage(name,to,image); //发送 } }
package rjsj.no19; public abstract class AbsChatroom { public abstract void register(Member member); public abstract void sendText(String form,String to,String message); public abstract void sendImage(String form,String to,String message); }
package rjsj.no19; import java.util.Hashtable; public class ChatRoom extends AbsChatroom{ private Hashtable members=new Hashtable(); @Override public void register(Member member) { if(!members.contains(member)) { members.put(member.getName(),member); member.setChatroom(this); } } @Override public void sendText(String form, String to, String message) { Member member = (Member) members.get(to); if (message.length() > 100) { System.out.println("文字长度大于100个字符,发送失败!"); } else { String newMessage = message.replaceAll("日","*");; member.receiveText(form,newMessage); } } @Override public void sendImage(String form, String to, String image) { Member member = (Member) members.get(to); //模拟图片大小判断 if(image.length()>20) { System.out.println("图片大于20M,发送失败!"); } else { member.receiveImage(form,image); } } }
package rjsj.no19; public class Client { public static void main(String[] args) { AbsChatroom happyChat = new ChatRoom(); Member member1,member2,member3,member4,member5; member1 = new DiamondMember("张三"); member2 = new DiamondMember("李四"); member3 = new CommonMember("王五"); member4 = new NewMember("小芳"); happyChat.register(member1); happyChat.register(member2); happyChat.register(member3); happyChat.register(member4); member1.sendText("李四","李四,你好!"); member2.sendText("张三","张三,你好!"); member1.sendText("李四","今天天气不错,有日!"); member2.sendText("张三","你说什么?"); member1.sendImage("李四","太阳.jpg"); member2.sendImage("张三","偷笑.jpg--------------------------------------------------------"); System.out.println("--------------------------------------------------------------------------------"); member3.sendText("小芳","听说聊天室新成员的信息会附带发送时间!"); member4.sendText("王五","哈哈"); } }
package rjsj.no19; public class CommonMember extends Member{ public CommonMember(String name) { super(name); } @Override public void sendText(String to, String message) { System.out.println("普通会员发送信息:"); chatroom.sendText(name,to,message); //发送 } @Override public void sendImage(String to, String image) { System.out.println("普通会员不能发送图片!"); } }
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); } }; class CrownMember :public Member{ protected : string name; public: CrownMember(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(); stringstream ss;//生成100个以上的字符 for(int i=0;i<110;i++) { ss <<"-"; } Member *member1,*member2,*member3,*member4,*member5,*member6; member1=new DiamondMember("张三"); member2=new DiamondMember("李四"); member3=new CommonMember("王五"); member4=new CommonMember("小芳"); member5=new CommonMember("小红"); member6=new CrownMember("小明"); newChat->registers(member1); newChat->registers(member2); newChat->registers(member3); newChat->registers(member4); newChat->registers(member5); newChat->registers(member6); member1->sendText("李四","李四,你好!"); member2->sendText("张三","张三,你好!"); member1->sendText("李四","今天天气不错,有日!"); member2->sendImage("张三","太阳"); member3->sendText("小芳","还有问题吗?"); member3->sendText("小红","还有问题吗?"); member4->sendText("王五","没有了,谢谢!"); member5->sendText("王五","我也没有了!"); member6->sendText("王五",ss.str()); }
运行结果: