中介者模式是一种行为型设计模式,用于减少对象之间的直接通信,而是通过一个中介对象来进行间接通信。这种模式有助于减少对象之间的耦合,使得系统更易于维护和扩展。
在中介者模式中,主要有以下几个角色:
① 中介者(Mediator) :定义了一个接口用于各个同事对象之间的通信,并负责实现具体的协调逻辑。
② 具体中介者(ConcreteMediator) :实现了中介者接口,负责协调各个同事对象之间的通信。
③ 同事类(Colleague) :每个同事类都知道中介者对象,并通过中介者对象来通信,而不是直接与其他同事对象通信。
④ 具体同事类(ConcreteColleague) :实现了同事类接口,每个具体同事类都需要知道中介者对象,并通过中介者对象来进行通信。
中介者模式通常应用于多个对象之间存在复杂的交互关系,通过引入中介者对象来简化对象之间的通信,降低耦合度,同时也使得系统更易于理解和扩展。
1、场景设计
实现场景:A、B两个同事通过中介者进行消息收发。
2、C++实现
`Colleague` 是同事类接口,定义了同事对象的基本行为。`ConcreteColleagueA` 和 `ConcreteColleagueB` 是具体的同事类,实现了同事类接口。`Mediator` 是中介者类接口,定义了中介者对象的基本行为。`ConcreteMediator` 是具体的中介者类,负责协调不同同事类之间的通信。在 `main` 函数中,我们创建了中介者对象和两个具体同事对象,并设置了中介者对象的引用,然后通过同事对象发送消息,中介者对象负责转发消息给其他同事对象。
cpp
#include <iostream>
#include <string>
#include <vector>
// 前置声明
class Mediator;
// 同事类接口
class Colleague {
protected:
Mediator* mediator;
public:
Colleague(Mediator* mediator) : mediator(mediator) {}
virtual void send(const std::string& message) = 0;
virtual void receive(const std::string& message) = 0;
};
// 中介者类接口
class Mediator {
public:
virtual void send(const std::string& message, Colleague* colleague) = 0;
};
// 具体同事类A
class ConcreteColleagueA : public Colleague {
public:
ConcreteColleagueA(Mediator* mediator) : Colleague(mediator) {}
void send(const std::string& message) override {
mediator->send(message, this);
}
void receive(const std::string& message) override {
std::cout << "ConcreteColleagueA received: " << message << std::endl;
}
};
// 具体同事类B
class ConcreteColleagueB : public Colleague {
public:
ConcreteColleagueB(Mediator* mediator) : Colleague(mediator) {}
void send(const std::string& message) override {
mediator->send(message, this);
}
void receive(const std::string& message) override {
std::cout << "ConcreteColleagueB received: " << message << std::endl;
}
};
// 具体中介者类
class ConcreteMediator : public Mediator {
private:
ConcreteColleagueA* colleagueA;
ConcreteColleagueB* colleagueB;
public:
void setColleagueA(ConcreteColleagueA* colleagueA) {
this->colleagueA = colleagueA;
}
void setColleagueB(ConcreteColleagueB* colleagueB) {
this->colleagueB = colleagueB;
}
void send(const std::string& message, Colleague* colleague) override {
if (colleague == colleagueA) {
colleagueB->receive(message);
} else if (colleague == colleagueB) {
colleagueA->receive(message);
}
}
};
int main() {
ConcreteMediator mediator;
ConcreteColleagueA colleagueA(&mediator);
ConcreteColleagueB colleagueB(&mediator);
mediator.setColleagueA(&colleagueA);
mediator.setColleagueB(&colleagueB);
colleagueA.send("Hello from colleague A");
colleagueB.send("Hi from colleague B");
return 0;
}
3、Java实现
`Mediator` 是中介者接口,定义了中介者对象的基本行为。`Colleague` 是同事类抽象类,定义了同事对象的基本行为。`ConcreteColleagueA` 和 `ConcreteColleagueB` 是具体的同事类,实现了同事类抽象类。`ConcreteMediator` 是具体的中介者类,负责协调不同同事类之间的通信。在 `main` 方法中,我们创建了中介者对象和两个具体同事对象,并将同事对象添加到中介者对象中,然后通过同事对象发送消息,中介者对象负责转发消息给其他同事对象。
java
package behavioralpattern.mediator;
import java.util.ArrayList;
import java.util.List;
// 中介者接口
interface Mediator {
void sendMessage(String message, Colleague colleague);
}
// 同事类抽象类
abstract class Colleague {
protected Mediator mediator;
public Colleague(Mediator mediator) {
this.mediator = mediator;
}
public abstract void sendMessage(String message);
public abstract void receiveMessage(String message);
}
// 具体同事类A
class ConcreteColleagueA extends Colleague {
public ConcreteColleagueA(Mediator mediator) {
super(mediator);
}
@Override
public void sendMessage(String message) {
mediator.sendMessage(message, this);
}
@Override
public void receiveMessage(String message) {
System.out.println("ConcreteColleagueA received: " + message);
}
}
// 具体同事类B
class ConcreteColleagueB extends Colleague {
public ConcreteColleagueB(Mediator mediator) {
super(mediator);
}
@Override
public void sendMessage(String message) {
mediator.sendMessage(message, this);
}
@Override
public void receiveMessage(String message) {
System.out.println("ConcreteColleagueB received: " + message);
}
}
// 具体中介者类
class ConcreteMediator implements Mediator {
private List<Colleague> colleagues = new ArrayList<>();
public void addColleague(Colleague colleague) {
colleagues.add(colleague);
}
@Override
public void sendMessage(String message, Colleague colleague) {
for (Colleague c : colleagues) {
if (c != colleague) {
c.receiveMessage(message);
}
}
}
}
public class MediatorDemo {
public static void main(String[] args) {
ConcreteMediator mediator = new ConcreteMediator();
ConcreteColleagueA colleagueA = new ConcreteColleagueA(mediator);
ConcreteColleagueB colleagueB = new ConcreteColleagueB(mediator);
mediator.addColleague(colleagueA);
mediator.addColleague(colleagueB);
colleagueA.sendMessage("Hello from colleague A");
colleagueB.sendMessage("Hi from colleague B");
}
}