abstract class AbstractColleague
{
public abstract void workSelf();
public abstract void requestOthersHelp();
}
class ColleagueA extends AbstractColleague
{
Mediator aMediator=new Mediator();
public void workSelf()
{
System.out.println("ColleagueA-->work");
}
public void requestOthersHelp()
{
System.out.println("ColleagueA-->request ColleagueB help");
aMediator.doRequest(this);
}
}
class ColleagueB extends AbstractColleague
{
Mediator aMediator=new Mediator();
public void workSelf()
{
System.out.println("ColleagueB-->work");
}
public void requestOthersHelp()
{
System.out.println("ColleagueB-->request ColleagueA help");
aMediator.doRequest(this);
}
}
class Mediator
{
AbstractColleague aAbstractColleague=null;
public void doRequest(AbstractColleague abstractColleague)
{
if(abstractColleague.getClass().equals(ColleagueA.class))
{
aAbstractColleague=new ColleagueB();
aAbstractColleague.workSelf();
}
else
{
aAbstractColleague=new ColleagueA();
aAbstractColleague.workSelf();
}
}
}
public class MediatorPatternTest
{
public static void main(String[] args)
{
AbstractColleague aColleagueA=new ColleagueA();
AbstractColleague aColleagueB=new ColleagueB();
//Mediator aMediator-new Mediator();
aColleagueA.workSelf();
aColleagueA.requestOthersHelp();
System.out.println("work done");
aColleagueB.workSelf();
aColleagueB.requestOthersHelp();
System.out.println("work done");
}
}