中介者模式源码应用
时间: 2023-10-23 22:09:36 浏览: 47
中介者模式是一种行为型设计模式,它通过将对象之间的交互委托给一个中介者对象来降低对象之间的耦合度。在实际开发中,中介者模式经常被用来处理复杂的交互关系,例如 GUI 应用程序中的窗口和控件之间的交互。
下面是一个简单的中介者模式的示例代码:
```java
// 抽象中介者类
public abstract class Mediator {
public abstract void send(String message, Colleague colleague);
}
// 抽象同事类
public abstract class Colleague {
protected Mediator mediator;
public Colleague(Mediator mediator) {
this.mediator = mediator;
}
public abstract void send(String message);
public abstract void receive(String message);
}
// 具体中介者类
public class ConcreteMediator extends Mediator {
private Colleague colleagueA;
private Colleague colleagueB;
public void setColleagueA(Colleague colleagueA) {
this.colleagueA = colleagueA;
}
public void setColleagueB(Colleague colleagueB) {
this.colleagueB = colleagueB;
}
@Override
public void send(String message, Colleague colleague) {
if (colleague == colleagueA) {
colleagueB.receive(message);
} else {
colleagueA.receive(message);
}
}
}
// 具体同事类A
public class ConcreteColleagueA extends Colleague {
public ConcreteColleagueA(Mediator mediator) {
super(mediator);
}
@Override
public void send(String message) {
mediator.send(message, this);
}
@Override
public void receive(String message) {
System.out.println("ConcreteColleagueA received message: " + message);
}
}
// 具体同事类B
public class ConcreteColleagueB extends Colleague {
public ConcreteColleagueB(Mediator mediator) {
super(mediator);
}
@Override
public void send(String message) {
mediator.send(message, this);
}
@Override
public void receive(String message) {
System.out.println("ConcreteColleagueB received message: " + message);
}
}
// 客户端代码
public class Client {
public static void main(String[] args) {
ConcreteMediator mediator = new ConcreteMediator();
ConcreteColleagueA colleagueA = new ConcreteColleagueA(mediator);
ConcreteColleagueB colleagueB = new ConcreteColleagueB(mediator);
mediator.setColleagueA(colleagueA);
mediator.setColleagueB(colleagueB);
colleagueA.send("Hello, colleagueB!");
colleagueB.send("Hi, colleagueA!");
}
}
```
在上面的示例中,中介者模式的核心是 `Mediator` 抽象类,它定义了一个抽象的 `send` 方法,用于将消息发送给其他的同事对象。具体的中介者类 `ConcreteMediator` 继承了 `Mediator` 类,并实现了其 `send` 方法,根据不同的同事对象来进行消息的转发。
同事对象 `Colleague` 抽象类定义了一个抽象的 `send` 方法和一个 `receive` 方法,用于发送和接收消息。具体的同事类 `ConcreteColleagueA` 和 `ConcreteColleagueB` 继承了 `Colleague` 类,并实现了其 `send` 和 `receive` 方法。
在客户端代码中,我们先创建了一个具体的中介者对象 `mediator`,然后分别创建了两个具体的同事对象 `colleagueA` 和 `colleagueB`,并将它们注册到中介者对象中。最后,通过调用同事对象的 `send` 方法来发送消息,并在控制台上输出接收到的消息。
总之,中介者模式可以帮助我们简化对象之间的交互,降低系统的耦合度,提高系统的可维护性和可扩展性。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)