实验19:中介者模式

实验19:中介者模式

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

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

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

 

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

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

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

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

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

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

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

实验要求:

1. 提交源代码;

2. 注意编程规范。

1.

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("普通会员不能发送图片!");

    }

}

posted @ 2023-11-22 10:51  哈哈哈老先生  阅读(11)  评论(0编辑  收藏  举报