Chain Of Responsibility



The super class for all command handler classes.
This class implements the set parent function to 
build up the chain.
This class implements the notify function to pass
the command up the chain.
The abstract handleCommand funtion will be implemented 
by sub classes.
-----------------------------------------------------
abstract class CommandHandler{
    private CommandHandler parent;

    public void setParent(CommandHandler parent){
        this.parent = parent;
    }

    public CommandHandler getParent(){
        return parent;
    }

    void notify(String event){
        if(!handleCommand(event)&& parent != null)
            parent.notify(event);
    }

    abstract boolean handleCommand(String event);
}
-----------------------------------------------------

The concrete command handlers follow. They implement
the handleCommand function with some sort of check to 
determine if the command is meant for this class.
A concrete command handler.
-----------------------------------------------------
public class CommandHandler1 extends CommandHandler{

    public boolean handleCommand(String command){
        if(command.equals("1")){
            System.out.println("CommandHandler1 to the rescue");
            return true;
        }
        return false;
    }
}
-----------------------------------------------------


A concrete command handler.
-----------------------------------------------------
public class CommandHandler2 extends CommandHandler{

    public boolean handleCommand(String command){
        if(command.equals("2")){
            System.out.println("CommandHandler2 to the rescue");
            return true;
        }
        return false;
    }
}
-----------------------------------------------------


A concrete command handler.
-----------------------------------------------------
public class CommandHandler3 extends CommandHandler{

    public boolean handleCommand(String command){
        if(command.equals("3")){
            System.out.println("CommandHandler3 to the rescue");
            return true;
        }
        return false;
    }
}
-----------------------------------------------------


This is the class that initiates the command up the 
chain.
This would map to a 'real world' device. When this 
'devices' state changes it may require a system response.
-----------------------------------------------------
public class CommandSender{
    private CommandHandler commandHandler;
    
    public void setCommandHandler(CommandHandler commandHandler){
        this.commandHandler = commandHandler;
    }

    public void doIt(String command){
        commandHandler.notify(command);
    }
}
-----------------------------------------------------


Test stub to run it all.
Notice calling doIt with an arg of "4" does not get
handled and causes no system change.
-----------------------------------------------------
class Test{

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

        //build up chain
        CommandHandler temp = new CommandHandler1();
        temp.setParent(new  CommandHandler3());
        CommandHandler chain = new  CommandHandler2();
        chain.setParent(temp);

        CommandSender commandSender = new CommandSender();
        commandSender.setCommandHandler(chain);
        
        commandSender.doIt("1");
        commandSender.doIt("2");
        commandSender.doIt("3");
        commandSender.doIt("4");
    }
}
-----------------------------------------------------


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