中介者设计模式
中介者设计模式是一种行为型设计模式,它通过一个中介者对象来封装一系列对象之间的交互关系,从而降低对象之间的耦合度。中介者对象充当了所有对象之间的协调者,它负责处理对象之间的通信和控制对象之间的行为。
优点:
-
降低耦合度:中介者对象将对象之间的交互关系封装起来,从而减少了对象之间的直接依赖关系,降低了耦合度,使得对象之间更加灵活和可扩展。
-
集中控制逻辑:中介者对象集中了对象之间的通信和行为控制逻辑,使得系统更加清晰和易于维护。
-
代码复用:中介者对象可以被多个对象共享和重用,从而减少了代码的冗余和重复。
缺点:
-
中介者对象过于复杂:中介者对象可能会变得非常复杂,因为它需要处理所有对象之间的通信和行为控制逻辑,从而增加了代码的维护成本和开发难度。
-
性能问题:中介者对象可能会成为系统的瓶颈,因为它需要处理所有对象之间的通信和行为控制逻辑,从而增加了系统的开销和响应时间。
下面是一个简单的 Java 代码示例,展示了如何使用中介者设计模式来协调多个对象之间的交互关系:
// 中介者接口
public interface Mediator {
void notify(Component sender, String event);
}
// 具体中介者实现类
public class ConcreteMediator implements Mediator {
private ComponentA componentA;
private ComponentB componentB;
public void setComponentA(ComponentA componentA) {
this.componentA = componentA;
}
public void setComponentB(ComponentB componentB) {
this.componentB = componentB;
}
@Override
public void notify(Component sender, String event) {
if (sender == componentA) {
System.out.println("Component A triggered event: " + event);
componentB.doSomething();
} else if (sender == componentB) {
System.out.println("Component B triggered event: " + event);
componentA.doSomething();
}
}
}
// 抽象组件类
public abstract class Component {
protected Mediator mediator;
public Component(Mediator mediator) {
this.mediator = mediator;
}
public void triggerEvent(String event) {
mediator.notify(this, event);
}
public abstract void doSomething();
}
// 具体组件A实现类
public class ComponentA extends Component {
public ComponentA(Mediator mediator) {
super(mediator);
}
@Override
public void doSomething() {
System.out.println("Component A do something.");
}
}
// 具体组件B实现类
public class ComponentB extends Component {
public ComponentB(Mediator mediator) {
super(mediator);
}
@Override
public void doSomething() {
System.out.println("Component B do something.");
}
}
在上面的例子中,我们定义了一个中介者接口 Mediator
,它有一个 notify
方法,用于处理组件之间的通信和行为控制逻辑。然后,我们创建了一个具体中介者实现类 ConcreteMediator
,它维护了两个组件对象 componentA
和 componentB
,并实现了 Mediator
接口的 notify
方法,用于处理组件之间的通信和行为控制逻辑。
接着,我们定义了一个抽象组件类 Component
,它有一个 triggerEvent
方法,用于触发事件,并将事件通知给中介者对象。具体组件类 ComponentA
和 ComponentB
分别继承了抽象组件类 Component
,并实现了 doSomething
方法,用于执行具体的业务逻辑。
最后,我们在客户端代码中创建了一个中介者对象 mediator
和两个组件对象 componentA
和 componentB
,并将它们之间的关系交给中介者对象进行协调。当组件对象 componentA
触发事件时,中介者对象 mediator
会接收到事件,并将事件通知给组件对象 componentB
,从而协调它们之间的交互关系。当组件对象 componentB
触发事件时,中介者对象 mediator
会接收到事件,并将事件通知给组件对象 componentA
,从而协调它们之间的交互关系。
jdk当中的使用
中介者模式在实际项目中的应用非常广泛,例如在电商平台中,订单服务作为中介者对象,协调买家服务和卖家服务之间的交互关系,从而实现订单的创建、支付、发货和确认等功能。又如,在游戏开发中,游戏场景作为中介者对象,协调各个游戏对象之间的交互关系,从而实现游戏的运行和逻辑控制。
下面是一个简单的 Java 代码示例,展示了如何使用中介者模式来协调多个对象之间的交互关系:
// 中介者接口
public interface Mediator {
void send(String message, Colleague colleague);
}
// 抽象同事类
public abstract class Colleague {
protected Mediator mediator;
public Colleague(Mediator mediator) {
this.mediator = mediator;
}
public abstract void receive(String message);
public abstract void send(String message);
}
// 具体同事类A
public class ConcreteColleagueA extends Colleague {
public ConcreteColleagueA(Mediator mediator) {
super(mediator);
}
@Override
public void receive(String message) {
System.out.println("ConcreteColleagueA received message: " + message);
}
@Override
public void send(String message) {
mediator.send(message, this);
}
}
// 具体同事类B
public class ConcreteColleagueB extends Colleague {
public ConcreteColleagueB(Mediator mediator) {
super(mediator);
}
@Override
public void receive(String message) {
System.out.println("ConcreteColleagueB received message: " + message);
}
@Override
public void send(String message) {
mediator.send(message, this);
}
}
// 具体中介者类
public class ConcreteMediator implements Mediator {
private ConcreteColleagueA colleagueA;
private ConcreteColleagueB colleagueB;
public void setColleagueA(ConcreteColleagueA colleagueA) {
this.colleagueA = colleagueA;
}
public void setColleagueB(ConcreteColleagueB colleagueB) {
this.colleagueB = colleagueB;
}
@Override
public void send(String message, Colleague colleague) {
if (colleague == colleagueA) {
colleagueB.receive(message);
} else if (colleague == colleagueB) {
colleagueA.receive(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, I am Colleague A.");
colleagueB.send("Hello, I am Colleague B.");
}
}
在上面的例子中,我们定义了一个中介者接口 Mediator
,它有一个 send
方法,用于处理同事对象之间的通信和协调关系。然后,我们创建了两个具体同事类 ConcreteColleagueA
和 ConcreteColleagueB
,它们分别继承了抽象同事类 Colleague
,并实现了 receive
方法和 send
方法,用于执行具体的业务逻辑。
接着,我们定义了一个具体中介者类 ConcreteMediator
,它维护了两个同事对象 colleagueA
和 colleagueB
,并实现了 Mediator
接口的 send
方法,用于处理同事对象之间的通信和协调关系。具体地,当同事对象 colleagueA
发送消息时,中介者对象 mediator
会将消息转发给同事对象 colleagueB
;当同事对象 colleagueB
发送消息时,中介者对象 mediator
会将消息转发给同事对象 colleagueA
。
最后,我们在客户端代码中创建了一个中介者对象 mediator
和两个同事对象 colleagueA
和 colleagueB
,并将同事对象的中介者属性设置为中介者对象 mediator
。当同事对象 colleagueA
和 colleagueB
分别发送消息时,中介者对象 mediator
会接收到消息,并将消息转发给对应的同事对象,从而协调它们之间的交互关系。
上述代码示例是一个简单的中介者模式的实现,它将同事对象之间的交互关系解耦,通过中介者对象来实现同事对象之间的通信和协调关系。在实际应用中,我们可以根据具体的业务需求和系统架构,使用中介者模式来协调多个对象之间的交互关系,从而实现系统的高性能、高可扩展性和高可维护性。
设计模式的过度使用
当系统中存在大量的对象需要进行交互时,过度使用中介者模式会引发以下问题:
-
代码复杂性增加:如果每个对象都通过中介者对象进行交互,中介者对象的复杂度会随着对象数量的增加而增加,导致代码复杂性增加,难以维护和扩展。
-
系统性能降低:中介者对象会成为系统的瓶颈,导致系统性能降低。
-
系统耦合度增加:使用中介者模式解耦对象之间的交互关系,会把所有交互关系都集中在中介者对象中,导致中介者对象和其他对象之间的依赖关系增加,系统耦合度增加。
因此,在使用中介者模式时,需要根据实际情况来决定是否使用,并注意控制中介者对象的复杂度和性能,避免过度使用中介者模式导致代码复杂性增加、系统性能降低和耦合度增加等问题。
文章评论