Power JavaDynamic Binder

Power Java
Dynamic Binder

Mike Mannion

Listing 1.
Example implementation of the getCommand method.


    protected Command getCommand(Target t)
    {
        return getCommandForClass(t.getClass());
    }
    protected Command getCommandForClass(Class targetClass)
    {
        if (commandTable.containsKey(targetClass))
        {
            // Return Command most suitable for this class
            return (Command)commandTable.get(targetClass);
        }
        else
        if (targetClass.equals(java.lang.Object.class))
        {
            // This should never happen
            throw new RuntimeException(
                "No command found for class "+targetClass.getName());
        }
        // Return updater corresponding to superclass of this class
        return getCommandForClass( targetClass.getSuperclass() );
    }