返回顶部

一缕半夏微光

温柔半两,从容一生

导航

软件设计19|中介者模式

一、Java版本

(1)效果如下:

(2)类图如下:

(3)工程目录如下:

(4)代码如下:

AbstractChatroom.java

1 package pattern;
2 
3 public abstract class AbstractChatroom {
4     public abstract void register(Member member);
5 
6     public abstract void sendText(String from, String to, String message);
7 
8     public abstract void sendImage(String from, String to, String image);
9 }

ChatGroup.java

 1 package pattern;
 2 
 3 import java.util.*;
 4 
 5 public class ChatGroup extends AbstractChatroom {
 6     @SuppressWarnings("rawtypes")
 7     private Hashtable members = new Hashtable();
 8 
 9     @SuppressWarnings("unchecked")
10     public void register(Member member) {
11         if (!members.contains(member)) {
12             members.put(member.getName(), member);
13             member.setChatroom(this);
14         }
15     }
16 
17     public void sendText(String from, String to, String message) {
18         Member member = (Member) members.get(to);
19         String newMessage = message;
20         newMessage = message.replaceAll("日", "*");
21         member.receiveText(from, newMessage);
22     }
23 
24     public void sendImage(String from, String to, String image) {
25         Member member = (Member) members.get(to);
26         // 模拟图片大小判断
27         if (image.length() > 5) {
28             System.out.println("图片太大,发送失败!");
29         } else {
30             member.receiveImage(from, image);
31         }
32     }
33 }

Client.java

 1 package pattern;
 2 
 3 public class Client {
 4     public static void main(String args[]) {
 5         AbstractChatroom happyChat = new ChatGroup();
 6         AbstractChatroom concreteChat=new ConcreteChat();
 7         
 8         Member member1,member2,member3,member4,member5,member6,member7;
 9         member1=new DiamondMember("张三");
10         member2=new DiamondMember("李四");
11         member3=new CommonMember("王五");
12         member4=new CommonMember("小芳");
13         member5=new CommonMember("小红");
14         member6=new ConcreteMembers("小李");//具体会员
15         member7=new ConcreteMembers("小王");//具体会员
16 
17         happyChat.register(member1);
18         happyChat.register(member2);
19         happyChat.register(member3);
20         happyChat.register(member4);
21         happyChat.register(member5);
22         concreteChat.register(member6);
23         concreteChat.register(member7);
24 
25         member1.sendText("李四","李四,你好!");
26         member2.sendText("张三","张三,你好!");
27         member1.sendText("李四","今天天气不错,有日!");
28         member2.sendImage("张三","一个很大很大的太阳");
29         member3.sendText("小芳","还有问题吗?");
30         member4.sendText("王五","没有了,谢谢!");
31         member5.sendText("王五","我也没有了!");
32         member5.sendImage("王五","谢谢");
33         
34         member6.sendText("小王", "你好鸭");
35         member6.sendImage("小王", "图片1");
36         member7.sendText("小李", "小李,你好鸭");
37         member7.sendImage("小李", "图片2");
38         
39     }
40 }

CommonMember.java

 1 package pattern;
 2 
 3 /**
 4  * 普通会员类
 5  * 
 6  * @author Administrator
 7  *
 8  */
 9 public class CommonMember extends Member {
10     public CommonMember(String name) {
11         super(name);
12     }
13 
14     public void sendText(String to, String message) {
15         System.out.println("普通会员发送信息:");
16         chatroom.sendText(name, to, message); // 发送
17     }
18 
19     public void sendImage(String to, String image) {
20         System.out.println("普通会员不能发送图片!");
21     }
22 }

ConcreteChat.java

 1 package pattern;
 2 
 3 import java.util.Hashtable;
 4 
 5 /**
 6  * 具体聊天室类
 7  * 
 8  * @author Administrator
 9  *
10  */
11 public class ConcreteChat extends AbstractChatroom {
12 
13     @SuppressWarnings("rawtypes")
14     private Hashtable members = new Hashtable();
15 
16     @SuppressWarnings("unchecked")
17     @Override
18     public void register(Member member) {
19         // TODO Auto-generated method stub
20         if (!members.contains(member)) {
21             members.put(member.getName(), member);
22             member.setChatroom(this);
23         }
24     }
25 
26     @Override
27     public void sendText(String from, String to, String message) {
28         // TODO Auto-generated method stub
29         Member member = (Member) members.get(to);
30         //发送的文字长度不得超过100个字符
31         if (message.length() > 100) // 假定100即为100个字符
32             System.out.println("图片太大,发送失败!");
33         else {
34             String newMessage = message;
35             newMessage = message.replaceAll("日", "*");
36             member.receiveText(from, newMessage);
37         }
38     }
39 
40     @Override
41     public void sendImage(String from, String to, String image) {
42         // TODO Auto-generated method stub
43         Member member = (Member) members.get(to);
44         // 图片大小判断
45         // 发送的图片大小不得超过20M
46         if (image.length() > 20) // 假定20即为20M
47             System.out.println("图片太大,发送失败!");
48         else
49             member.receiveImage(from, image);
50 
51     }
52 
53 }

ConcreteMembers.java

 1 package pattern;
 2 
 3 import java.text.SimpleDateFormat;
 4 import java.util.Date;
 5 
 6 /**
 7  * 具体会员类
 8  * 
 9  * @author Administrator
10  *
11  */
12 public class ConcreteMembers extends Member {
13 
14     public ConcreteMembers(String name) {
15         super(name);
16         // TODO Auto-generated constructor stub
17     }
18 
19     @Override
20     public void sendText(String to, String message) {
21         // TODO Auto-generated method stub
22         // 获取当前系统时间
23         Date date = new Date();
24         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
25         System.out.println("具体会员发送信息:");
26         chatroom.sendText(name, to, message+"("+format.format(date)+")"); // 发送
27     }
28 
29     @Override
30     public void sendImage(String to, String image) {
31         // TODO Auto-generated method stub
32         System.out.println("具体会员发送图片:");
33         chatroom.sendImage(name, to, image); // 发送
34     }
35 
36 }

DiamondMember.java

 1 package pattern;
 2 
 3 /**
 4  * 钻石会员类
 5  * 
 6  * @author Administrator
 7  *
 8  */
 9 public class DiamondMember extends Member {
10     public DiamondMember(String name) {
11         super(name);
12     }
13 
14     public void sendText(String to, String message) {
15         System.out.println("钻石会员发送信息:");
16         chatroom.sendText(name, to, message); // 发送
17     }
18 
19     public void sendImage(String to, String image) {
20         System.out.println("钻石会员发送图片:");
21         chatroom.sendImage(name, to, image); // 发送
22     }
23 }

Member.java

 1 package pattern;
 2 
 3 /**
 4  * 会员
 5  * 
 6  * @author Administrator
 7  *
 8  */
 9 public abstract class Member {
10     protected AbstractChatroom chatroom;
11     protected String name;
12 
13     public Member(String name) {
14         this.name = name;
15     }
16 
17     public String getName() {
18         return name;
19     }
20 
21     public void setName(String name) {
22         this.name = name;
23     }
24 
25     public AbstractChatroom getChatroom() {
26         return chatroom;
27     }
28 
29     public void setChatroom(AbstractChatroom chatroom) {
30         this.chatroom = chatroom;
31     }
32 
33     public abstract void sendText(String to, String message);
34 
35     public abstract void sendImage(String to, String image);
36 
37     public void receiveText(String from, String message) {
38         System.out.println(from + "发送文本给" + this.name + ",内容为:" + message);
39     }
40 
41     public void receiveImage(String from, String image) {
42         System.out.println(from + "发送图片给" + this.name + ",内容为:" + image);
43     }
44 }

参考链接:中介者模式实例与解析----实例:虚拟聊天室

二、C++版本

(1)效果如下:

(2)类图如下:

(3)代码如下:

  1 #include<iostream>
  2 #include <map>
  3 #include<ctime>
  4 #include <windows.h> 
  5 #include <stdio.h>
  6 using namespace std;
  7 
  8 class Member;
  9 //抽象中介者类AbstractChatroom(抽象聊天室类) 
 10 class AbstractChatroom {
 11 public:
 12     virtual void registerMember(Member* member) = 0;
 13     virtual void sendText(string from, string to, string message) = 0;
 14     virtual void sendImage(string from, string to, string image) = 0;
 15 };
 16 
 17 //抽象同事类Member(抽象会员类) 
 18 class Member {
 19 public:
 20     Member(string name) {
 21         this->name = name;
 22     }
 23 
 24     string getName() {
 25         return this->name;
 26     }
 27 
 28     void setName(string name) {
 29         this->name = name;
 30     }
 31 
 32     AbstractChatroom* getChatroom() {
 33         return chatroom;
 34     }
 35 
 36     void setChatroom(AbstractChatroom* chatroom) {
 37         this->chatroom = chatroom;
 38     }
 39 
 40     virtual void sendText(string to, string message) = 0;
 41     virtual void sendImage(string to, string image) = 0;
 42 
 43     void receiveText(string from, string message) {
 44         SYSTEMTIME st;//定义本地时间变量,该变量为结构体
 45         GetLocalTime(&st);//获取本地时间函数,参数为时间变量指针
 46         cout << from << "发送文本给" << this->name << ",内容为:" << message;
 47         cout << "(" << st.wYear << "-" << st.wMonth << "-" << st.wDay << " " << st.wHour << ":" << st.wMinute << ")" << endl;
 48     }
 49 
 50     void receiveImage(string from, string image) {
 51         cout << from << "发送图片给" << this->name << ",内容为:" << image << endl;
 52     }
 53 
 54 protected:
 55     AbstractChatroom* chatroom;
 56     string name;
 57 };
 58 
 59 //具体中介者类Chatgroup(具体聊天室类) 
 60 class ChatGroup : public AbstractChatroom {
 61 public:
 62     void registerMember(Member* member) {
 63         if (!members.count(member->getName())) {
 64             members.insert(make_pair(member->getName(), member));
 65             member->setChatroom(this);
 66         }
 67     }
 68 
 69     void sendText(string from, string to, string message) {
 70         Member* member = members[to];
 71         member->receiveText(from, message);
 72     }
 73 
 74     void sendImage(string from, string to, string image) {
 75         Member* member = members[to];
 76         if (image.length() > 5) {
 77             cout << "图片太大,发送失败!" << endl;
 78         }
 79         else {
 80             member->receiveImage(from, image);
 81         }
 82 
 83     }
 84 private:
 85     map<string, Member*> members;
 86 };
 87 
 88 //新的具体聊天室类
 89 class NewChatGroup : public AbstractChatroom {
 90 public:
 91     void registerMember(Member* member) {
 92         if (!members.count(member->getName())) {
 93             members.insert(make_pair(member->getName(), member));
 94             member->setChatroom(this);
 95         }
 96     }
 97 
 98     void sendText(string from, string to, string message) {
 99         Member* member = members[to];
100         if (message.length() > 100 ){//新的具体聊天室中发送的文字长度不得超过100个字符.假定100即为100个字符
101             cout << "发送的文字长度超过100个字符,发送失败!" << endl;
102         }
103         else {
104             member->receiveText(from, message);
105         }
106     }
107 
108     void sendImage(string from, string to, string image) {
109         Member* member = members[to];
110         if (image.length() > 20) {//新的具体聊天室中发送的图片大小不得超过20M。假定20即为20M
111             cout << "图片大小超过20M,发送失败!" << endl;
112         }
113         else {
114             member->receiveImage(from, image);
115         }
116 
117     }
118 private:
119     map<string, Member*> members;
120 };
121 
122 //具体同事类CommonMember(普通会员类)
123 class CommonMember : public Member {
124 public:
125     CommonMember(string name) : Member(name) {
126 
127     }
128 
129     void sendText(string to, string message) {
130         cout << "普通会员发送信息:" << endl;
131         chatroom->sendText(name, to, message);
132     }
133 
134     void sendImage(string to, string image) {
135         cout << "普通会员不能发送图片!" << endl;
136     }
137 };
138 
139 //具体同事类DiamondMember(钻石会员类)
140 class DiamondMember : public Member {
141 public:
142     DiamondMember(string name) : Member(name) {
143 
144     }
145 
146     void sendText(string to, string message) {
147         cout << "钻石会员发送信息:" << endl;
148         chatroom->sendText(name, to, message);
149     }
150 
151     void sendImage(string to, string image) {
152         cout << "钻石会员发送图片:" << endl;
153         chatroom->sendImage(name, to, image);
154     }
155 };
156 
157 //新的具体会员类
158 class ConcreteMember : public Member {
159 public:
160     ConcreteMember(string name) : Member(name) {
161 
162     }
163 
164     void sendText(string to, string message) {
165         cout << "具体会员发送信息:" << endl;
166         chatroom->sendText(name, to, message);
167     }
168 
169     void sendImage(string to, string image) {
170         cout << "具体会员发送图片:" << endl;
171         chatroom->sendImage(name, to, image);
172     }
173 };
174 
175 //客户端测试
176 int main(void) {
177     //创建聊天室 
178     ChatGroup happyChat;
179     NewChatGroup newhappyChat;
180 
181     //创建成员
182     Member* member1=new CommonMember("张三");
183     Member* member2=new DiamondMember("李四");
184     Member* member3 = new ConcreteMember("王五");
185     Member* member4 = new ConcreteMember("小李");
186 
187     //注册 
188     happyChat.registerMember(member1);
189     happyChat.registerMember(member2);
190     newhappyChat.registerMember(member3);
191     newhappyChat.registerMember(member4);
192 
193     //发送信息
194     member1->sendText("李四", "李四,你好!");
195     member2->sendText("张三", "张三,你好!");
196     member1->sendText("李四", "今天天气很不错");
197     member1->sendImage("李四", "好天气");
198     member2->sendImage("张三", "一个很大的太阳");
199     member2->sendImage("张三", "太阳");
200     member3->sendText("小李", "早上好呀");
201     member3->sendImage("小李", "图片1");
202     member4->sendText("王五", "王五,吃饭了嘛");
203     member4->sendImage("王五", "图片2");
204 
205     return 0;
206 
207 }

参考链接:设计模式之中介者模式实例(c++)

posted on 2021-11-23 21:30  一缕半夏微光  阅读(31)  评论(0编辑  收藏  举报