I'm exploring possibilities of static and default methods introduced in java 8.
I've an interface that has 2 default methods that do construct a command, that I run on server through ssh to do some simple tasks remotely. Moving mouse requires 2 arguments: x and y position of the mouse.
public interface Robot {
default String moveMouse(int x, int y) {
return constructCmd("java -jar move_mouse.jar " + x + " " + y);
}
default String clickLeft() {
return constructCmd("java -jar click_left.jar");
}
static String constructCmd(String cmd) {
return "export DISPLAY=:1.0\n" +
"cd Desktop\n" +
cmd;
}
}
I've multiple enums with values preset, I could potently combine all enums into one and not use interface what so ever, however that enum would contain hundreds or thousands of values and I want to keep it somewhat organised, so I've split evertying in multiple enums.
I want all enums to share same methods so I figured I'll give default methods in an interface a shot.
public enum Field implements Robot {
AGE_FIELD(778, 232),
NAME_FIELD(662, 280);
public int x;
public int y;
Field(int x, int y) {
this.x = x;
this.y = y;
}
}
So I can get String commands by:
Field.AGE_FIELD.clickLeft();
Field.AGE_FIELD.moveMouse(Field.AGE_FIELD.x, Field.AGE_FIELD.y);
However moveMouse looks really bad to me and I think it should be somehow possible to use enum's values by default.
Anyone has a a nice solution for such problem?
void moveMouse(),int getMouseTargetX()andint getMouseTargetY(). Your current design asks for the specific x- and y-Coordinates. My proposal assumes that the object has some attributes, defininig the x- and y- Coordinates. My proposal is more like an extension to your already existing system than a replacement. Your interface would be used for the component to actually move the mouse, whereas my interface would be used by components, that call the component that actually moves the mouse.