2021.11.18 中介者模式

一、今日学习内容

   

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

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

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

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

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

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

 

类图:

 

 

 

测试结果:

Java

 

 

 

抽象同事者类

package com.song;
//抽象同事者类
public abstract class Colleague
{
//定义一个中介者
protected Mediator mediator;
//定义会员的名字
protected String name;
//构造函数
public Colleague(){}
public Colleague(String name) {
this.name = name;
}

public String getName() {
return name;
}

public Mediator getMediator() {
return mediator;
}

public void setMediator(Mediator mediator) {
this.mediator = mediator;
}

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 sender,String text)
{
System.out.println(sender + "发送文本给" + this.name + "\n内容为:" + text);
}

public void receiveImage(String sender,String image)
{
System.out.println(sender+ "发送图片给" + this.name + "\n内容为:" + image);
}

}

 

具体同事者类
普通会员类:

package com.song;

public class CommonMember extends Colleague {
public CommonMember(String name) {
super(name);
}

@Override
public void sendText(String receiver, String text) {
mediator.sendText(name,receiver,text);
}

@Override
public void sendImage(String receiver, String image) {
System.out.println("普通会员不能发送图片!");
}
}

 

钻石会员类:

package com.song;

public class DiamondMember extends Colleague {
public DiamondMember(String name) {
super(name);
}

@Override
public void sendText(String receiver, String text) {
mediator.sendText(name,receiver,text);
}

@Override
public void sendImage(String receiver, String image) {
mediator.sendImage(name,receiver,image);
}
}

 

抽象中介者类

package com.song;

import java.util.HashMap;
import java.util.Map;

//抽象中介者类
public abstract class Mediator {
//定义会员集合
protected Map<String,Object> colleagues=new HashMap<String,Object>();
//注册会员
public void register(Colleague colleague)
{
colleagues.put(colleague.getName(),colleague);
colleague.setMediator(this);
}
public abstract void sendText(String sender,String receiver,String text);
public abstract void sendImage(String sender,String receiver,String image);

}

 

具体中介者类:

package com.song;

public class ConcreteMediator extends Mediator {
@Override
public void sendText(String sender, String receiver, String text) {
//通过传入的接收者的名字,获取接收者对象
Colleague receiver1= (Colleague) colleagues.get(receiver);
//对不雅字符进行过滤,如将“日”字符,用“*”代替
String text1=text.replaceAll("日","*");
receiver1.receiveText(sender,text1);
}

@Override
public void sendImage(String sender, String receiver, String image) {
Colleague receiver1= (Colleague) colleagues.get(receiver);
//对发送图片大小进行控制
if(image.length()>15){
System.out.println("图片太大,发送失败");
}
else{
receiver1.receiveImage(sender,image);
}
}
}

 

客户类

package com.song;

public class Client {
public static void main(String[] args) {
Mediator chatRoom=new ConcreteMediator();
Colleague colleague1,colleague2;

colleague1=new CommonMember("张三");
colleague2=new DiamondMember("李四");

chatRoom.register(colleague1);
chatRoom.register(colleague2);

//测试普通会员的发送权限
System.out.println("-----------------------------测试普通会员的发送权限---------------------------\n");
colleague1.sendText("李四","我是普通会员,向你发送文本!");
colleague1.sendImage("李四","我是普通会员,向你发送图片!");


//测试钻石会员发送权限
System.out.println("\n------------------------------测试钻石会员发送权限-----------------------------\n");
colleague2.sendText("张三","我是钻石会员,向你发送文本");
colleague2.sendImage("张三","我是钻石会员,向你发送图片!");

//测试过滤不雅字符
System.out.println("\n----------------------------------测试过滤不雅字符-------------------------------\n");
colleague1.sendText("李四","测试日");

//测试发送图片大小
System.out.println("\n-----------------------------测试发送图片大小---------------------------------\n");
colleague2.sendImage("张三","测试发送图片大小测试发送图片大小测试发送图片大小测试发送图片大小");
}
}

 

posted @ 2021-11-18 21:30  小仙女W  阅读(101)  评论(0编辑  收藏  举报