複数のオブジェクト(Colleague)の制御を行うのにMediatorクラスを作成し、これを介して制御を行います。
package org.example.mediator;
public interface Mediator {
public void createColleagues();
public void colleagueChanged();
}
package org.example.mediator;
public abstract class Colleague {
protected Mediator mediator;
public abstract void setMediator(Mediator mediator);
public abstract void controlColleague();
}
package org.example.mediator;
public class ConcreteColleague1 extends Colleague {
@Override
public void setMediator(Mediator mediator) {
this.mediator = mediator;
}
@Override
public void controlColleague() {
}
}
package org.example.mediator;
public class ConcreteColleague2 extends Colleague {
@Override
public void setMediator(Mediator mediator)
{
this.mediator = mediator;
}
@Override
public void controlColleague()
{
}
}
package org.example.mediator;
public class ConcreteMediator implements Mediator{
private Colleague concreteColleague1;
private Colleague concreteColleague2;
@Override
public void createColleagues() {
concreteColleague1 = new ConcreteColleague1();
concreteColleague1.setMediator(this);
concreteColleague2 = new ConcreteColleague1();
concreteColleague2.setMediator(this);
}
@Override
public void colleagueChanged() {
concreteColleague1.controlColleague();
concreteColleague2.controlColleague();
}
}
package org.example.mediator;
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Mediator mediator = new ConcreteMediator();
mediator.createColleagues();
mediator.colleagueChanged();
}
}