I assume that you want something like array of methods. There are two ways of achieving this. First way is by using reflection. It requires less coding, but reduces performance, since reflection in java is performance greedy. Second way (more preferable) is Command Pattern. I've had situation like yours and tested reflection vs command pattern performance. Command pattern is about 30 - 50ms faster per 1000 calls. It may seem not a big difference, but you can notice it if your program will call that methods frequently (graphics updating, modules interaction etc. .)
CommandPattern
Processor
interface ProcessorMethod{
public void invoke();
}
ProcessorsFactory
class ProcessorsFactory{
private Processor processor;
private HashMap<Integer, ProcessorMethod> processors;
public ProcessorsFactory(Processor p){
processor = p;
processors = new HashMap<>();
processors.put(new Integer(0), new ProcessorMethod(){
@Override
public void invoke(){
processor.processFunction_0();}
});
processors.put(new Integer(1), new ProcessorMethod(){
@Override
public void invoke(){
processor.processFunction_1();}
});
.
.
.
}
public ProcessorMethod getProcessor(int processorIndex){
return processors.get(new Integer(processorIndex));
}
Processor
class Processor{
public void processFunction_1() {.......}
public void processFunction_2() {.......}
.
.
.
}
Some Class
Class SomeClass{
ProcessorsFactory factory;
Processor processor;
public SomeClass(){
processor = new Processor();
factory = new ProcessorsFactory(processor);
}
public void doSomething(int processingFunction){
factory.getProcessor(processingFunction).invoke();
}
}
Reflection
ProcessorsFactory
class ProcessorsFactory{
private HashMap<Integer, Method> processingFunctions = new HashMap<>();
public ProcessorsFactory(Processor processors){
Class processorsClass = processors.getClass();
int count = 0;
for(Method m : processorsClass.getDeclaredMethods())
if(m.getName().startsWith("processFunction")
processingFunctions.put(new Integer(count++), m)
}
}
public Method getProcessor(int processorIndex){
return processingFunctions.get(new Integer(processorIndex))};
}
Processor
class Processor{
public void processFunction_1() {.......}
public void processFunction_2() {.......}
.
.
.
}
SomeClass
Class SomeClass{
ProcessorsFactory factory;
Processor processor;
public SomeClass(){
processor = new Processor();
factory = new ProcessorsFactory(processor);
}
public void doSomething(int processingFunction){
factory.getProcessor(processingFunction).invoke(processor, arg1, arg2. . .)
}
}
If you will not gonna use these methods very frequently, fell free to use Reflections. If you want better performance, choose Command Pattern.
P.S
This is not copy-paste pieces of code, but principles, that I know and had used. You should refactor it by your needs. I recommend you to learn
Design Patterns. Derek Banas youtube chanell is a good place to start. He know how to explain such things. Cheers :)