中介者模式(Mediator Pattern)是一种行为型设计模式,旨在通过定义一个中介者对象来封装一系列对象之间的交互,从而使这些对象之间的通信不再直接发生,降低它们之间的耦合度。中介者模式通过将对象间的复杂通信封装到一个中介者对象中,使得各对象之间的交互变得简单、灵活和可维护。
中介者模式主要涉及以下几个角色:
下面是中介者模式的UML类图示例:
+------------------+
| Mediator |
+------------------+
| +mediate() |
+------------------+
^
|
|
+------------------+
| ConcreteMediator |
+------------------+
| -colleague1 |
| -colleague2 |
| +mediate() |
+------------------+
^ ^
| |
| |
+------------------+ +------------------+
| ConcreteColleague1| | ConcreteColleague2|
+------------------+ +------------------+
| -mediator | | -mediator |
| +operation() | | +operation() |
+------------------+ +------------------+
让我们从一个简单的示例开始,假设我们有两个同事对象,它们需要通过一个中介者对象进行通信。
// Mediator Interface
public interface Mediator {
void mediate();
}
// ConcreteMediator
public class ConcreteMediator implements Mediator {
private ConcreteColleague1 colleague1;
private ConcreteColleague2 colleague2;
public void setColleague1(ConcreteColleague1 colleague1) {
this.colleague1 = colleague1;
}
public void setColleague2(ConcreteColleague2 colleague2) {
this.colleague2 = colleague2;
}
@Override
public void mediate() {
colleague1.operation();
colleague2.operation();
}
}
// Colleague1 Interface
public interface Colleague {
void setMediator(Mediator mediator);
void operation();
}
// ConcreteColleague1
public class ConcreteColleague1 implements Colleague {
private Mediator mediator;
@Override
public void setMediator(Mediator mediator) {
this.mediator = mediator;
}
@Override
public void operation() {
System.out.println("ConcreteColleague1 operation.");
}
}
// ConcreteColleague2
public class ConcreteColleague2 implements Colleague {
private Mediator mediator;
@Override
public void setMediator(Mediator mediator) {
this.mediator = mediator;
}
@Override
public void operation() {
System.out.println("ConcreteColleague2 operation.");
}
}
// Main class to demonstrate
public class Main {
public static void main(String[] args) {
ConcreteMediator mediator = new ConcreteMediator();
ConcreteColleague1 colleague1 = new ConcreteColleague1();
ConcreteColleague2 colleague2 = new ConcreteColleague2();
colleague1.setMediator(mediator);
colleague2.setMediator(mediator);
mediator.setColleague1(colleague1);
mediator.setColleague2(colleague2);
mediator.mediate();
}
}
在实际应用中,中介者模式可以用于复杂的系统中,例如GUI系统的事件处理。下面是一个GUI事件处理的例子,其中按钮和文本框通过一个中介者对象进行交互:
// Mediator Interface
public interface Mediator {
void buttonPressed();
void textChanged();
}
// ConcreteMediator
public class GUIChatMediator implements Mediator {
private Button button;
private TextBox textBox;
public void setButton(Button button) {
this.button = button;
}
public void setTextBox(TextBox textBox) {
this.textBox = textBox;
}
@Override
public void buttonPressed() {
textBox.setText("Button was pressed");
}
@Override
public void textChanged() {
button.setEnabled(!textBox.getText().isEmpty());
}
}
// Button
public class Button {
private Mediator mediator;
private boolean enabled = true;
public Button(Mediator mediator) {
this.mediator = mediator;
}
public void press() {
mediator.buttonPressed();
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
System.out.println("Button enabled: " + enabled);
}
}
// TextBox
public class TextBox {
private Mediator mediator;
private String text = "";
public TextBox(Mediator mediator) {
this.mediator = mediator;
}
public void setText(String text) {
this.text = text;
mediator.textChanged();
}
public String getText() {
return text;
}
}
// Main class to demonstrate
public class Main {
public static void main(String[] args) {
GUIChatMediator mediator = new GUIChatMediator();
Button button = new Button(mediator);
TextBox textBox = new TextBox(mediator);
mediator.setButton(button);
mediator.setTextBox(textBox);
textBox.setText("Hello");
button.press();
}
}
中介者模式适用于以下场景:
中介者模式是一种强大且灵活的设计模式,可以有效地减少对象之间的耦合度,集中管理对象间的交互。然而,在使用中介者模式时,也需要注意避免中介者成为单点故障,并在适当的场景下使用该模式以保持系统的简单性和可维护性。通过本文的示例代码和讲解,希望你对Java中的中介者模式有了深入的理解,并能够在实际项目中有效地应用这一模式。