1: import java.util.ArrayList;
2:
3: abstract class AbstractMediator{
4: public abstract void register(AbstractColleague ac);
5: public abstract void ColleagueChanged(AbstractColleague ac);
6: }
7: abstract class AbstractColleague{
8: private AbstractMediator med;
9: public AbstractColleague(AbstractMediator mediator){
10: this.med=mediator;
11: }
12: public abstract void action();
13: public void changed(){
14: med.ColleagueChanged(this);
15: }
16: }
17: class ConcreteMediator extends AbstractMediator{
18:
19: private ArrayList<AbstractColleague> colleagueList=new ArrayList<AbstractColleague>();
20: public void register(AbstractColleague ac) {
21: colleagueList.add(ac);
22: }
23:
24: public void ColleagueChanged(AbstractColleague ac) {
25: for(int i=0;i<colleagueList.size();i++){
26: if(colleagueList.get(i)!=ac){
27: colleagueList.get(i).action();
28: }
29: }
30: }
31: }
32:
33: class ConcreteColleagueA extends AbstractColleague{
34: public ConcreteColleagueA(AbstractMediator mediator){
35: super(mediator);
36: mediator.register(this);
37: }
38: public void action() {
39: System.out.println("AAAAAAAAAAAAAAA");
40: }
41: }
42: class ConcreteColleagueC extends AbstractColleague{
43: public ConcreteColleagueC(AbstractMediator mediator){
44: super(mediator);
45: mediator.register(this);
46: }
47: public void action() {
48: System.out.println("CCCCCCCCCCCCCCC");
49: }
50: }
51: class ConcreteColleagueB extends AbstractColleague{
52: public ConcreteColleagueB(AbstractMediator mediator){
53: super(mediator);
54: mediator.register(this);
55: }
56: public void action() {
57: System.out.println("BBBBBBBBBBBBBBB");
58: }
59: }
60: class test{
61: public static void main(String[] args){
62: AbstractMediator mediator=new ConcreteMediator();
63: AbstractColleague colleagueA=new ConcreteColleagueA(mediator);
64: AbstractColleague colleagueB=new ConcreteColleagueB(mediator);
65: AbstractColleague colleagueC=new ConcreteColleagueC(mediator);
66: colleagueA.changed();
67: colleagueB.changed();
68: colleagueC.changed();
69: }
70: }
71: