B08 中介者模式 示例

本文深入探讨了中介者模式在通信场景中的应用,通过抽象中介者和成员类的设计,实现了成员间解耦的通信机制。具体介绍了如何使用Java实现中介者模式,包括抽象中介者、抽象成员、具体中介者和具体成员的职责划分,以及客户端如何利用中介者进行通信。
摘要由CSDN通过智能技术生成

示例类图

8195388-da1079002c817c83.png
中介者模式.png

示例代码

抽象中介者
  • 定义了中介者为成员提供的服务;
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);

}
抽象成员类
  • 在抽象成员类中建立了成员和中介者的关系,成员要知道哪个中介者为我服务;
public abstract class Member {

    protected AbstractChatRoom chatRoom;

    protected String name;

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

    public AbstractChatRoom getChatRoom() {
        return chatRoom;
    }

    public void setChatRoom(AbstractChatRoom 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);
    }

}
具体中介者
  • 成员间的通信都委托给了中介者了;
  • 成员注册到中介者中,并且成员也知道哪个中介者为我服务;
import java.util.Hashtable;

public class ChatGroup extends AbstractChatRoom{

    private Hashtable<String, Member> 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 from, String to, String message) {
        Member toMember = members.get(to);
        String clearedMessage = message.replaceAll("日", "*");
        toMember.receiveImage(from, clearedMessage);
    }

    @Override
    public void sendImage(String from, String to, String image) {
        Member toMember = members.get(to);
        if (image.length() > 15) {
            System.out.println("图片太大,发送失败");
        } else {
            toMember.receiveImage(from, image);
        }
    }

}
具体成员类
  • 具体成员间的通信委托给了中介者;
public class CommonMember extends Member{

    public CommonMember(String name) {
        super(name);
    }

    @Override
    public void sendText(String to, String message) {
        System.out.println("普通会员发送文本:");
        chatRoom.sendText(this.name, to, message);
    }

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

}

public class VIPMember extends Member {

    public VIPMember(String name) {
        super(name);
    }

    @Override
    public void sendText(String to, String message) {
        System.out.println("VIP会员发送文本:");
        chatRoom.sendText(this.name, to, message);
    }

    @Override
    public void sendImage(String to, String image) {
        System.out.println("VIP会员发送图片:");
        chatRoom.sendImage(this.name, to, image);
    }
    
}
客户端
  • 成员注册进中介者;
  • 成员间的通信委托给中介者;
public class Test {
    public static void main(String[] args) {
        AbstractChatRoom chatRoom = new ChatGroup();

        Member member1 = new VIPMember("张三");
        Member member2 = new VIPMember("李四");
        Member member3 = new CommonMember("王五");
        Member member4 = new CommonMember("小芳");
        Member member5 = new CommonMember("小红");

        chatRoom.register(member1);
        chatRoom.register(member2);
        chatRoom.register(member3);
        chatRoom.register(member4);
        chatRoom.register(member5);

        member1.sendText("李四", "你好");
    }
}

输出:

VIP会员发送文本:
张三发送图片给李四,内容为:你好

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值