源自
Mediator中介者模式
用一個中介者對象來封裝一系列的對象交互
例子
view plaincopy to clipboardprint?
public abstract class AbstractMediator {
public abstract void register(AbstractColleague ac);
public abstract void ColleagueChanged(AbstractColleague ac);
}
public class ConcreteMediator extends AbstractMediator {
private ArrayList<ABSTRACTCOLLEAGUE></ABSTRACTCOLLEAGUE> colleagueList = new ArrayList<ABSTRACTCOLLEAGUE></ABSTRACTCOLLEAGUE>();
public void register(AbstractColleague ac) {
colleagueList
}
public void ColleagueChanged(AbstractColleague ac) {
for (int i =
if (colleagueList
colleagueList
}
}
}
}
public abstract class AbstractColleague {
private AbstractMediator med;
public AbstractColleague(AbstractMediator mediator) {
d = mediator;
}
public abstract void action();
public void changed() {
med
}
}
public class ConcreteColleagueA extends AbstractColleague {
public ConcreteColleagueA(AbstractMediator mediator) {
super(mediator);
mediator
}
public void action() {
System
}
}
public class ConcreteColleagueB extends AbstractColleague {
public ConcreteColleagueB(AbstractMediator mediator) {
super(mediator);
mediator
}
public void action() {
System
}
}
public class ConcreteColleagueC extends AbstractColleague {
public ConcreteColleagueC(AbstractMediator mediator) {
super(mediator);
mediator
}
public void action() {
System
}
}
public class Client {
public static void main(String[] args) {
AbstractMediator mediator = new ConcreteMediator();
AbstractColleague colleagueA = new ConcreteColleagueA(mediator);
AbstractColleague colleagueB = new ConcreteColleagueB(mediator);
AbstractColleague colleagueC = new ConcreteColleagueC(mediator);
colleagueA
colleagueB
colleagueC
}
}
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26381.html