I’m working on something that controls a physical installation.
The installation exists of a grid that can have each cell either black or white.
Atm i have this method:
void programRunner(int message) {
if (message == STARTSHOWTIME) {
println("showtime");
}
if (message == ENDSHOWTIME) {
println("parse new state");
boolean[][] nextState = randomState();
gridCommunicator.nextState(nextState);
checkMatrix.set(nextState);
}
}
It passes a multidimensional array where true is white and false is black.
It get’s called whenever the installation is ready with setting something in position and when going to the next.
However there should be multiple modes, one with the random black and white, other that draws letters for example, one that shows images etc.
I would like to have each mode as a object, i only don’t know what i’m exactly looking for.
Take for example:
class Program {
String programName;
Program(String programName) {
this.programName = programName;
}
// . . . . . . . . . . . . . . . . . . .
void programRunner(int message) {
if (message == STARTSHOWTIME) {
println("showtime");
}
if (message == ENDSHOWTIME) {
println("parse new state");
boolean[][] nextState = randomState();
gridCommunicator.nextState(nextState);
checkMatrix.set(nextState);
}
}
// . . . . . . . . . . . . . . . . . . .
}
the method programRunner is now still the same, and to have a different one i have to make a new class. But i don’t want a new class, i prefer that i can create the object and pass to it how the method programRunner should look like.
Is something like that possible in java?
If you need to customize the behavior of a method with a few changing parts,
A sketch of the solution follows.
The interface:
Your method:
Calling the method: