设计模式之中介者模式(行为型)

本文介绍了中介者模式的基本概念,包括其结构和实现方式。通过一个简单的示例展示了如何使用中介者模式来降低对象间的耦合度,使得各对象之间通过中介者进行通信,从而简化了对象之间的交互关系。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、中介者模式介绍

中介者模式(Mediator),用一个中介对象来封装一系列的对象交互。中介者使个对象不需要显式的互相引用,从而使其松耦合,而且可以独立地改变它们之间的交互

中介者模式很好的体现了迪米特法则,如果两个类不必彼此直接通信,那个这两个类就不应当发生直接的相互作用,如果一个类要调用另一个类的某个方法,可以通过第三者来转发这个调用。

2、中介者模式结构

在这里插入图片描述

通过中介者对象,可以将相互调用的类之间的网状结构,变成以中介者为中心的星型结构,每个具体对象通过中介者与其他对象发生相互作用

中介者模式角色:

  • Colleague:抽象同事类
  • ConcreteColleague:具体同事类,每个具体同事只知道自己的行为,而不了解其它同事类的情况,但他们都认识中介者对象
  • Mediator:抽象中介者,定义了同事对象到中介者对象的接口
  • ConcreteMediator:具体中介者对象,实现抽象类的方法,需要知道所有的具体同事类,并从具体同事接收消息,向具体同事对象发出命令
Mediator
/**
 * 中介者
 */
public interface Mediator {

    /**
     * 发送消息给具体同事
     */
    void sendMessage(String message, Colleague colleague);

}
Colleague
/**
 * 同事类
 */
public interface Colleague {

    /**
     * 发送消息
     */
    void send(String message, Mediator mediator);

    /**
     * 接收中介者的消息
     */
    void notify(String message);

}
ConcreteMediator
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 具体中介者
 */
@Service
public class ConcreteMediator implements Mediator {

    /**
     * 管理具体的同事类
     */
    @Resource
    List<Colleague> colleagueList = new ArrayList<>();

    @Override
    public void sendMessage(String message, Colleague colleague) {
        for (Colleague col : colleagueList) {
            if (col == colleague) {
                // 对于自身的消息可以做其它逻辑处理
                // doSomething...
                continue;
            }
            // 其它参与者通知
            col.notify(message);
        }
    }

}
ConcreteColleague
import org.springframework.stereotype.Service;

/**
 * 具体同事 A
 */
@Service("colleagueA")
public class ConcreteColleagueA implements Colleague {

    public void send(String message, Mediator mediator) {
        mediator.sendMessage(message, this);
    }

    public void notify(String message) {
        System.out.println("ConcreteColleagueA 得到消息 " + message);
    }

}


/**
 * 具体同事 B
 */
@Service("colleagueB")
public class ConcreteColleagueB implements Colleague {

    public void send(String message, Mediator mediator) {
        mediator.sendMessage(message, this);
    }

    public void notify(String message) {
        System.out.println("ConcreteColleagueB 得到消息 " + message);
    }

}

调用方:
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;

@SpringBootTest
@RunWith(SpringRunner.class)
public class ApplicationTest {

    @Resource(name = "colleagueA")
    private Colleague concreteColleagueA;

    @Resource(name = "colleagueB")
    private Colleague concreteColleagueB;

    @Resource
    private Mediator mediator;

    @Test
    public void test() {
        // 具体同事 A 发送消息
        mediator.sendMessage("明天吃什么?", concreteColleagueA);
        // result:ConcreteColleagueB 得到消息 明天吃什么?

        // 具体同事 B 发送消息
        mediator.sendMessage("明天吃面吧。", concreteColleagueB);
        // result:ConcreteColleagueA 得到消息 明天吃面吧。
    }

}

3、中介者模式优缺点

  1. 中介者模式减少了各个 Colleague 之间的耦合,而且将中介作为一个独立的概念封装到一个对象中,关注的对象就会从对象本身的行为转移到他们之间的交互上来。
  2. 交互由各个类之间集中到了具体中介者对象中,使交互复杂性变为了中介者的复杂性,使中介者变得比任何一个 ConcreteColleague 都复杂,使用中介者模式时要考虑到对象之间交互的复杂度和中介类本身的复杂度
  3. 中介者模式设计思想通过引入中间层,将一组对象之间的交互关系从网状(多对多)转换为星状(一对多),减少了对象之间的交互关系,降低了代码的复杂度,提高了代码的可读性和可维护性。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一起来搬砖呀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值