Mediator


When there are complex interdependencies between objects based 
on their state the mediator pattern uses one object to coordinate 
state changes between the interdependent objects.


The mediator class.
This class will coridinate state changes between
button objects.
Button objects will register with the Mediator to be 
informed of state changes or (as in this case) to have 
thier state changed as a result of state changes in 
other button objects.
The 'state' dependent logic is very simple in this 
case, only one button is allowed to be "off" at once.
-----------------------------------------------------
class Mediator implements EventListner{

    Vector vector = new Vector();

    public void register(Object obj){
        vector.add(obj);
    }

    public void handelIt(Object obj){
        for(int i = 0; i<vector.size(); i++){
            Button temp = (Button)vector.get(i);
            if(temp!=obj && temp.getState().equals("off"))
                temp.turnOn();
        }
    }
}
-----------------------------------------------------


The interface to decouple mediator from the button
objects.
-----------------------------------------------------
public interface EventListner{
    public void register(Object obj);
    public void handelIt(Object obj);
}
-----------------------------------------------------


The button class.
Button objects will set the private variable 'listner'
to be the mediator class so it can be informed of 
pertinent state changes. 
( in this case being "turned off" )
-----------------------------------------------------
class Button{
   
    private String state;
    private EventListner listner;

    public Button(String state,EventListner listner){
        this.state = state;
        this.listner = listner;
        listner.register(this);
    }

    public void turnOn(){
        this.state = "on";
    }

    public void turnOff(){
        this.state = "off";
        listner.handelIt(this);
    }

    public String getState(){
        return state;
    }

    public String report(){
        return  state;
    } 
}
-----------------------------------------------------


A test stub to make it all work.
-----------------------------------------------------
class Test{

    public static void main(String[] args) throws Exception{
        System.out.println("class: Test, method: main");

        Mediator mediator = new Mediator();
        Button button1 = new Button("on",mediator);
        Button button2 = new Button("on",mediator);
        Button button3 = new Button("off",mediator);

        System.out.println();
        System.out.println(button1.report());
        System.out.println(button2.report());
        System.out.println(button3.report());
        Thread.sleep(1000);

        button1.turnOff();
        System.out.println();
        System.out.println(button1.report());
        System.out.println(button2.report());
        System.out.println(button3.report());
        Thread.sleep(1000);

        button2.turnOff();
        System.out.println();
        System.out.println(button1.report());
        System.out.println(button2.report());
        System.out.println(button3.report());
        Thread.sleep(1000);

        button3.turnOff();
        System.out.println();
        System.out.println(button1.report());
        System.out.println(button2.report());
        System.out.println(button3.report());
        Thread.sleep(1000);
    }
}
-----------------------------------------------------



Here is a tar with the above java classes in it. 
mediator.tar