中介者模式
public abstract class Mediator{
public abstract void register(String colleagueName,Colleague colleague);
public abstract void getMessage(int stateChange,String colleague);
public void sendMessage();
}
public class ConcreteMediator extends Mediator{
private HashMap<String,Colleague> colleagueMap;
private HashMap<String,String> interMap;
public ConcreteMediator(){
colleagueMap = new HashMap<String,Colleague>();
interMap = new HashMap<String,String>();
}
public void register(String colleagueName,Colleague colleague){
colleagueMap.put(colleagueName,colleague);
if(colleague instanceof Alarm){
interMap.put("Alarm",colleagueName);
}else if(colleague instanceof TV){
interMap.put("TV",colleagueName);
}
}
public void getMessage(int stateChange,String colleague){
if(colleagueMap.get(ColleagueName) instanceof Alarm){
if(stateChange == 0) {
((TV)(colleagueMap.get(interMap.get("TV")))).startTv();
}else if(stateChange == 1){
((TV)(colleagueMap.get(interMap.get("TV")))).stopTv();
}
}else if(colleagueMap.get(ColleagueName) instanceof TV){
}
}
}
puhblic abstract class Colleague{
private Mediator mediator;
public String name;
public Colleague(Mediator mediator,String name){
this.mediator = mediator;
this.name = name;
}
public Mediator getMediator(){
return mediator;
}
public bastract void sendMessage(int stateChange);
}
public class Alarm extends Colleague{
public Alarm(Mediator mediator,String name){
super(mediator,name);
mediator.register(name,this);
}
public void sendAlarm(int stateChange){
sendMessage(stateChange);
}
public void sendMessage(int stateChange){
this.getMediator().getMessage(stateChange,name);
}
}
public class TV extends Colleague{
public Alarm(Mediator mediator,String name){
super(mediator,name);
mediator.register(name,this);
}
public void sendAlarm(int stateChange){
sendMessage(stateChange);
}
public void sendMessage(int stateChange){
this.getMediator().getMessage(stateChange,name);
}
public void startTv(){
}
public void stopTv(){
}
}