具体代码
//AbstractChatroom.java
package org.example.test017;
import javax.swing.plaf.basic.BasicTreeUI;
public abstract class AbstractChatroom {
public abstract void register(Member member);
public abstract void sendText(String from, String to, String message);
public abstract void sendImage(String from, String to, String image);
}
//ChatGroup.java
package org.example.test017;
import java.util.Hashtable;
public class ChatGroup extends AbstractChatroom {
private Hashtable members = new Hashtable();
@Override
public void register(Member member) {
if (!members.contains(member)) {
members.put(member.getName(), member);
member.setAbstractChatroom(this);
}
}
@Override
public void sendText(String from, String to, String message) {
Member member = (Member)members.get(to);
String newMessage = message;
newMessage = message.replaceAll("日", "*");
member.receiveText(from, newMessage);
}
@Override
public void sendImage(String from, String to, String image) {
Member member = (Member)members.get(to);
if (image.length() > 5) {
System.out.println("图片太大,发送失败!");
}
else {
member.receiveImage(from, image);
}
}
}
//NewGroup.java
package org.example.test017;
import java.util.HashMap;
import java.util.Map;
public class NewGroup extends AbstractChatroom {
Map<String, Member> table = new HashMap<>();
@Override
public void register(Member member) {
table.put(member.getName(), member);
member.setAbstractChatroom(this);
}
@Override
public void sendText(String from, String to, String message) {
if (message.length() > 100) {
System.out.println("文本长度超过100,发送失败!");
}
else {
Member member = table.get(to);
if (member != null) {
member.receiveText(from, message);
}
}
}
@Override
public void sendImage(String from, String to, String image) {
if (image.length() > 20) {
System.out.println("图片大小超过20,发送失败!");
}
else {
Member member = table.get(to);
if (member != null) {
member.receiveImage(from, image);
}
}
}
}
//Member.java
package org.example.test017;
public abstract class Member {
protected AbstractChatroom abstractChatroom;
protected String name;
public Member(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public AbstractChatroom getAbstractChatroom() {
return abstractChatroom;
}
public void setAbstractChatroom(AbstractChatroom abstractChatroom) {
this.abstractChatroom = abstractChatroom;
}
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);
}
}
//CommonMember.java
package org.example.test017;
public class CommonMember extends Member {
public CommonMember(String name) {
super(name);
}
@Override
public void sendText(String to, String message) {
System.out.println("普通会员发送信息:");
abstractChatroom.sendText(name, to, message);
}
@Override
public void sendImage(String to, String image) {
System.out.println("普通会员不能发送图片!");
}
}
//DiamondMember.java
package org.example.test017;
public class DiamondMember extends Member {
public DiamondMember(String name) {
super(name);
}
@Override
public void sendText(String to, String message) {
System.out.println("钻石会员发送信息:");
abstractChatroom.sendText(name, to, message);
}
@Override
public void sendImage(String to, String image) {
System.out.println("钻石会员发送图片:");
abstractChatroom.sendImage(name, to, image);
}
}
//NewMember.java
package org.example.test017;
public class NewMember extends Member {
public NewMember(String name) {
super(name);
}
@Override
public void sendText(String to, String message) {
System.out.println("新会员发送文本消息:");
abstractChatroom.sendText(name, to, message);
}
@Override
public void sendImage(String to, String image) {
System.out.println("新会员发送图片:");
abstractChatroom.sendImage(name, to, image);
}
}
//Client.java
package org.example.test017;
public class Client {
public static void main(String[] args) {
AbstractChatroom abstractChatroom = new NewGroup();
Member m1, m2, m3;
m1 = new NewMember("张三");
m2 = new NewMember("李四");
m3 = new NewMember("王五");
abstractChatroom.register(m1);
abstractChatroom.register(m2);
abstractChatroom.register(m3);
m1.sendText("李四", "早上好奥~");
m2.sendText("张三", "早上好!");
m2.sendImage("王五", "早上好.jpg");
m3.sendImage("李四", "很高兴见到你~~~~~~~~~~~~~~~~~~~~~~~~~~~.jpg");
}
}