I have a remote object exposed through RMI, which I use to perform several things on a resource that the object holds. Right now I have method for each operation I need to perform.
Example:
public interface IRobotController {
public int walk(int meters);
public void dance(int seconds);
}
public interface RMIRobotController implements IRobotController, java.rmi.Remote {}
public class RobotController implements RMIRobotController {
private Robot robot;
...
public int walk(int meters) {
return robot.walk(meters);
}
public void dance(int seconds) {
robot.dance(seconds);
}
}
I would like refactor the API so that I would only have one method, operate()
. Assume I don't want to return an actual reference to the robot object through RMI, because I sometimes need to perform more than one operation, and don't want the round trips.
I came up with this, based on the concept of Callable<T>
:
public abstract class RobotCallable<T> {
protected Robot robot;
public void setRobot(Robot robot) {
this.robot = robot;
}
public abstract T call()
}
public class RobotController implements RMIRobotController {
private Robot robot;
...
public T operate(RobotCallable<T> robotCallable) {
robotCallable.setRobot(robot)
return robotCallable.call();
}
}
I this how these things are usually implemented? Is there some mechanism for RMI that does this automatically?