public class Mediator {
Welcome welcome;
Browse browse;
Purchase purchase;
Exit exit;
public Mediator() {
welcome = new Welcome(this);
browse = new Browse(this);
purchase = new Purchase(this);
exit = new Exit(this);
}
public void handle(String state) {
if (state.equals("welcome.shop")) {
browse.execute();
} else if (state.equals("shop.purchase")) {
purchase.execute();
} else if (state.equals("purchase.exit")) {
exit.execute();
} else if (state.equals("welcome.exit")) {
exit.execute();
} else if (state.equals("shop.exit")) {
exit.execute();
} else if (state.equals("purchase.exit")) {
exit.execute();
}
}
public Welcome getWelcome() {
return welcome;
}
public static void main(String args[]) {
Mediator mediator = new Mediator();
mediator.getWelcome().execute();
}
}
class Exit {
Mediator mediator;
public Exit(Mediator m) {
mediator = m;
}
public void execute() {
System.out.println("exit");
}
}
class Browse {
Mediator mediator;
public Browse(Mediator m) {
mediator = m;
}
public void execute() {
mediator.handle("shop.purchase");
mediator.handle("shop.exit");
}
}
class Welcome {
Mediator mediator;
public Welcome(Mediator m) {
mediator = m;
}
public void execute() {
mediator.handle("welcome.shop");
mediator.handle("welcome.exit");
}
}
class Purchase {
Mediator mediator;
public Purchase(Mediator m) {
mediator = m;
}
public void execute() {
System.out.println("Thanks for your purchase.");
mediator.handle("purchase.exit");
}
}
|