菜鸟的博客

纵有疾风起,人生不言弃。

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

软件设计-Tutorial19

package Tutorial19;

// 中介者接口
public interface Mediator {
    void sendMessage(AbstractChatRoom chatRoom, AbstractMember member, String message);

    void sendPicture(AbstractChatRoom chatRoom, AbstractMember member, byte[] picture);
}
复制代码
package Tutorial19;

// 抽象聊天室类
public abstract class AbstractChatRoom {
    protected Mediator mediator;

    public AbstractChatRoom(Mediator mediator) {
        this.mediator = mediator;
    }

    public abstract void sendMessage(AbstractMember member, String message);

    public abstract void sendPicture(AbstractMember member, byte[] picture);
}
复制代码
复制代码
package Tutorial19;

// 抽象会员类
public abstract class AbstractMember {
    protected String name;
    protected AbstractChatRoom chatRoom;

    public AbstractMember(String name) {
        this.name = name;
    }

    public void setChatRoom(AbstractChatRoom chatRoom) {
        this.chatRoom = chatRoom;
    }

    public abstract void sendMessage(String message);

    public abstract void sendPicture(byte[] picture);
}
复制代码
复制代码
package Tutorial19;

// 新的具体聊天室类,限制发送图片大小和文字长度
public class NewChatRoom extends AbstractChatRoom {
    private static final int MAX_PICTURE_SIZE = 20 * 1024 * 1024; // 20M
    private static final int MAX_TEXT_LENGTH = 100;

    public NewChatRoom(Mediator mediator) {
        super(mediator);
    }

    @Override
    public void sendMessage(AbstractMember member, String message) {
        if (message.length() <= MAX_TEXT_LENGTH) {
            mediator.sendMessage(this, member, message);
        } else {
            System.out.println("发送的文字长度超过限制,无法发送。");
        }
    }

    @Override
    public void sendPicture(AbstractMember member, byte[] picture) {
        if (picture.length <= MAX_PICTURE_SIZE) {
            mediator.sendPicture(this, member, picture);
        } else {
            System.out.println("发送的图片大小超过限制,无法发送。");
        }
    }
}
复制代码
复制代码
package Tutorial19;

// 新的具体会员类,可以发送图片和文本信息,并在文本信息后添加时间
import java.text.SimpleDateFormat;
import java.util.Date;

public class NewMember extends AbstractMember {
    public NewMember(String name) {
        super(name);
    }

    @Override
    public void sendMessage(String message) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String timestampedMessage = message + " [" + sdf.format(new Date()) + "]";
        chatRoom.sendMessage(this, timestampedMessage);
    }

    @Override
    public void sendPicture(byte[] picture) {
        chatRoom.sendPicture(this, picture);
    }
}
复制代码
复制代码
package Tutorial19;

// 客户端测试类
public class ClientTest {
    public static void main(String[] args) {
        // 创建中介者对象
        Mediator mediator = new Mediator() {
            @Override
            public void sendMessage(AbstractChatRoom chatRoom, AbstractMember member, String message) {
                System.out.println(member.name + " 在 " + chatRoom.getClass().getSimpleName() + " 发送文本消息:" + message);
            }

            @Override
            public void sendPicture(AbstractChatRoom chatRoom, AbstractMember member, byte[] picture) {
                System.out.println(member.name + " 在 " + chatRoom.getClass().getSimpleName() + " 发送图片消息");
            }
        };

        // 创建新的具体聊天室
        NewChatRoom newChatRoom = new NewChatRoom(mediator);

        // 创建新的具体会员
        NewMember member1 = new NewMember("会员1");
        NewMember member2 = new NewMember("会员2");

        // 设置会员所属的聊天室
        member1.setChatRoom(newChatRoom);
        member2.setChatRoom(newChatRoom);

        // 会员发送文本消息
        member1.sendMessage("这是一条不超过100字符的消息");
        member2.sendMessage("我也发一条消息试试");

        // 模拟发送图片(这里只是示例,实际应替换为真实的图片字节数组)
        byte[] samplePicture = new byte[10 * 1024 * 1024]; // 10M大小的示例图片字节数组
        member1.sendPicture(samplePicture);
    }
}
复制代码

 

posted on   hhmzd233  阅读(3)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示