Java中介者模式简介

什么是中介者模式?

中介者模式(Mediator Pattern)是一种行为型模式,用于降低对象之间的耦合度。在中介者模式中,多个对象之间不直接相互通信,而是通过一个中介者对象进行交互。中介者对象封装了对象之间的关系,使对象之间的通信更加简洁明了。

中介者模式的结构

中介者模式包含以下几个角色:

  • Mediator(抽象中介者):定义了对象之间交互的接口,可以是一个抽象类或接口。
  • ConcreteMediator(具体中介者):实现中介者接口,协调各个具体同事类之间的交互关系。
  • Colleague(抽象同事类):定义了对象之间交互的接口,维护了一个中介者对象的引用。
  • ConcreteColleague(具体同事类):实现了抽象同事类的接口,通过中介者与其他同事类进行交互。

下面是中介者模式的UML类图示例:

classDiagram
    class Mediator {
        +colleagueChanged(colleague: Colleague): void
    }
    class ConcreteMediator {
        +colleagueChanged(colleague: Colleague): void
        +registerColleague(colleague: Colleague): void
    }
    abstract class Colleague {
        -mediator: Mediator
        +Colleague(mediator: Mediator)
        +send(message: String): void
        +receive(message: String): void
    }
    class ConcreteColleague1 {
        +ConcreteColleague1(mediator: Mediator)
        +send(message: String): void
        +receive(message: String): void
    }
    class ConcreteColleague2 {
        +ConcreteColleague2(mediator: Mediator)
        +send(message: String): void
        +receive(message: String): void
    }
    Mediator <|-- ConcreteMediator
    Colleague <|-- ConcreteColleague1
    Colleague <|-- ConcreteColleague2

代码示例

下面以一个简单的聊天室为例,演示中介者模式的使用。

首先,我们定义一个抽象中介者类Mediator,其中包含一个抽象方法colleagueChanged用于处理同事类的交互:

public abstract class Mediator {
    public abstract void colleagueChanged(Colleague colleague);
}

然后,实现一个具体中介者类ConcreteMediator,它继承自抽象中介者类,并实现了colleagueChanged方法来处理同事类的交互关系:

public class ConcreteMediator extends Mediator {
    private List<Colleague> colleagues = new ArrayList<>();

    public void registerColleague(Colleague colleague) {
        colleagues.add(colleague);
    }

    public void colleagueChanged(Colleague colleague) {
        for (Colleague c : colleagues) {
            if (c != colleague) {
                c.receive(colleague.getClass().getSimpleName() + ": " + colleague.getMessage());
            }
        }
    }
}

接下来,定义一个抽象同事类Colleague,其中包含一个中介者对象的引用,并提供了发送和接收消息的方法:

public abstract class Colleague {
    protected Mediator mediator;
    protected String message;

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

    public void send(String message) {
        this.message = message;
        mediator.colleagueChanged(this);
    }

    public abstract void receive(String message);
}

最后,实现具体的同事类ConcreteColleague,它继承自抽象同事类,并实现了receive方法来处理接收到的消息:

public class ConcreteColleague1 extends Colleague {
    public ConcreteColleague1(Mediator mediator) {
        super(mediator);
    }

    public void receive(String message) {
        System.out.println("ConcreteColleague1 received: " + message);
    }
}

public class ConcreteColleague2 extends Colleague {
    public ConcreteColleague2(Mediator mediator) {
        super(mediator);
    }

    public void receive(String message) {
        System.out.println("ConcreteColleague2 received: " + message);
    }
}

现在,我们可以在客户端代码中使用中介者模式来协调同事类的交互:

public class Client {
    public static void main(String[] args) {
        ConcreteMediator mediator = new