//中介者 interface Mediator{ public void send(String msg, Person person); } //具体中介者 class ConcreteMediator implements Mediator{ //中介者所联系的交互对象 private PersonA personA; private PersonA personB; public void setPersonA(PersonA personA){ this.personA = personA; } Public void setPersonB(PersonB personB){ this.personB = personB; } public void send(String msg, Person person){ if(person.equals(personA)){ personA.greeting(msg); }else{ personB.greeting(msg); } } } //抽象同事 abstract class Person{ //同事和中介者打交道 protected Mediator mediator; public Person(Mediator mediator){ this.mediator = mediator; } } //具体同事 class PersonA extends Person{ public PersonA(Mediator mediator){ super(mediator); } public void send(String msg){ mediator.send(msg, this); } public void greeting(string msg){ System.out.println(“PersonA: ” + msg); } } class PersonB extends Person{ public PersonB(Mediator mediator){ super(mediator); } public void send(String msg){ mediator.send(msg, this); } public void greeting(string msg){ System.out.println(“PersonB: ” + msg); } } public class MediatorDemo{ public static void main(String[] args){ //创建中介 Mediator mediator = new ConcreteMediator(); //创建同事,并为同事设置中介 Person personA = new PersonA(mediator); Person personB = new PersonB(mediator);