今日收获

实验19:中介者模式

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

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

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

 

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

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

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

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

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

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

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

 

package shiyan19;

public abstract class Abcharroom {
    public abstract void register(Member m);
    public abstract void sendtext(String a,String b,String mess);
    public abstract void sendimage(String a,String b,String mess);
}

 

package shiyan19;

import java.util.*;

public class Group extends Abcharroom{
    private Hashtable ms=new Hashtable();
    public void register(Member m)
    {
        if(!ms.contains(m)){
            ms.put(m.getname(),m);
            m.setroom(this);
        }
    }
    public void sendtext(String a,String b,String c){
        Member m=(Member)ms.get(b);
        String newm=c;
        newm=c.replaceAll("日","*");
        m.receivetext(a,newm);
    }
    public void sendimage(String a,String b,String c){
        Member m=(Member)ms.get(b);
        if(c.length()>5){
            System.out.println("图片较大,发送失败");
        }else{
            m.receiveimage(a,c);
        }
    }
}
package shiyan19;

public class main {
    public static void main(String args[]){
        Abcharroom r1=new Room();
        Member m1,m2,m3;
        m1=new Xin("A");
        m2=new Xin("B");
        m3=new Xin("C");
        r1.register(m1);
        r1.register(m2);
        r1.register(m3);
        m1.sendtext("B",":玩什么玩,代码敲了吗");
        m2.sendtext("A",":就你能说");
        m2.sendtext("A",":才加上你把---------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
        m1.sendtext("B","呵呵");
        m3.sendimage("A",":坏笑.jpg-----------------------------------------");
        m3.sendimage("B",":坏笑.jpg");
        
    }
}
package shiyan19;

public abstract class Member {
    protected Abcharroom room;
    protected String name;
    public Member(String a){
        name=a;
    }
    public String getname(){
        return name;
    }
    public void setname(String a){
        name=a;
    }
    public Abcharroom getroom(){
        return room;
    }
    public void setroom(Abcharroom a){
        room=a;
    }
    public abstract void sendtext(String a,String mess);
    public abstract void sendimage(String a,String image);
    public void receivetext(String a,String mess){
        System.out.println(a+"发送的给"+name+"内容是"+mess);
    }
    public void receiveimage(String a,String image){
        System.out.println(a+"发送的给"+name+"内容是"+image);
    }
}
package shiyan19;


public class Pu extends Member{
    public Pu(String a){
        super(a);
    }
    public void sendtext(String a,String c){
        System.out.println("普通会员发送消息");
        room.sendtext(name,a,c);
    }
    public void sendimage(String a,String c){
        System.out.println("普通会员不能发送图片");
    }
}
package shiyan19;

import java.util.HashMap;
import java.util.Map;
public class Room extends Abcharroom{

        Map<String,Member> table = new HashMap<>();
        public void register(Member member) {
            table.put(member.getname(), member);
            member.setroom(this);
        }

        public void sendtext(String from, String to, String message) {
            if(message.length()>=100){
                System.err.println("超过100个字符,发送失败");
            }else{
                Member member = table.get(to);
                if(member!=null)
                    member.receivetext(from, message);
            }
        }

        public void sendimage(String from, String to, String message) {
            if(message.length()>=20){
                System.err.println("图片超过20M,发送失败");
            }else{
                Member member = table.get(to);
                if(member!=null)
                    member.receiveimage(from, message);
            }
        }

}
package shiyan19;

import java.util.HashMap;
import java.util.Map;
public class Room extends Abcharroom{

        Map<String,Member> table = new HashMap<>();
        public void register(Member member) {
            table.put(member.getname(), member);
            member.setroom(this);
        }

        public void sendtext(String from, String to, String message) {
            if(message.length()>=100){
                System.err.println("超过100个字符,发送失败");
            }else{
                Member member = table.get(to);
                if(member!=null)
                    member.receivetext(from, message);
            }
        }

        public void sendimage(String from, String to, String message) {
            if(message.length()>=20){
                System.err.println("图片超过20M,发送失败");
            }else{
                Member member = table.get(to);
                if(member!=null)
                    member.receiveimage(from, message);
            }
        }

}
package shiyan19;

public class Xin extends Member{
    public Xin(String a){
        super(a);
    }
    public void sendtext(String a,String c){
        System.out.println("新会员发送消息");
        room.sendtext(name,a,c);
    }
    public void sendimage(String a,String c){
        System.out.println("新会员不能发送图片");
        room.sendimage(name,a,c);
    }
}
package shiyan19;

public class Zuan extends Member{
    public Zuan(String a){
        super(a);
    }
    public void sendtext(String a,String c){
        System.out.println("钻石会员发送消息");
        room.sendtext(name,a,c);
    }
    public void sendimage(String a,String c){
        System.out.println("钻石会员发送图片");
        room.sendimage(name,a,c);
    }
}

 

posted @ 2021-11-10 21:51  敲敲代代码码  阅读(101)  评论(0编辑  收藏  举报