一句话总结
星状结构
内容
由对象直接耦合变成对象和中介者进行关联,中介者在于其他对象进行关联,将网状结构变成星状结构。
场景
朋友圈,信息交易平台
类图
代码示例
// 抽象同事类
public abstract class Colleague {
protected Mediator mediator;
public Colleague(Mediator mediator) {
this.mediator = mediator;
}
}
// 具体同事类
public class ConcreteColleagueA extends Colleague {
public ConcreteColleagueA(Mediator mediator) {
super(mediator);
this.mediator.setColleageA(this);
}
// 自有方法:self-Method
public void selfMethodA() {
// 处理自己的逻辑
System.out.println(String.format("%s:self-Method", this.getClass().getSimpleName()));
}
// 依赖方法:dep-Method
public void depMethodA() {
// 处理自己的逻辑
System.out.println(String.format("%s:depMethod: delegate to Mediator", this.getClass().getSimpleName()));
// 无法处理的业务逻辑委托给中介者处理
this.mediator.transferA();
}
}
// 具体同事类
public class ConcreteColleagueB extends Colleague {
public ConcreteColleagueB(Mediator mediator) {
super(mediator);
this.mediator.setColleageB(this);
}
// 自有方法:self-Method
public void selfMethodB() {
// 处理自己的逻辑
System.out.println(String.format("%s:self-Method", this.getClass().getSimpleName()));
}
// 依赖方法:dep-Method
public void depMethodB() {
// 处理自己的逻辑
System.out.println(String.format("%s:depMethod: delegate to Mediator", this.getClass().getSimpleName()));
// 无法处理的业务逻辑委托给中介者处理
this.mediator.transferB();
}
}
// 抽象中介者
public abstract class Mediator {
protected ConcreteColleagueA colleagueA;
protected ConcreteColleagueB colleagueB;
public void setColleageA(ConcreteColleagueA colleague) {
this.colleagueA = colleague;
}
public void setColleageB(ConcreteColleagueB colleague) {
this.colleagueB = colleague;
}
// 中介者业务逻辑
public abstract void transferA();
public abstract void transferB();
}
// 具体中介者
public class ConcreteMediator extends Mediator {
@Override
public void transferA() {
// 协调行为:A 转发到 B
this.colleagueB.selfMethodB();
}
@Override
public void transferB() {
// 协调行为:B 转发到 A
this.colleagueA.selfMethodA();
}
}
public class Test {
public static void main(String[] args) {
Mediator mediator = new ConcreteMediator();
ConcreteColleagueA colleagueA = new ConcreteColleagueA(mediator);
ConcreteColleagueB colleagueB = new ConcreteColleagueB(mediator);
colleagueA.depMethodA();
System.out.println("-------------------------");
colleagueB.depMethodB();
}
}
运行结果