I am building a matching tool in Java for a university project which basically just consists of an ensemble of several simpler matching algorithms. Now I want people to be able to easily add their own algorithms and automatically include them into the ensemble by just adding the .java files into the project folder.
Each algorithm must implements a MatchingComponentInterface and I want to achieve that each class implementing this interface tells the ensemble that it exists so that the ensemble can dynamically assemble itself including that algorithm.
For a simplified example let the main and ensemble code look like this:
class Main {
@Getter
private Ensemble ensemble = new Ensemble();
public static void main(String[] args){
//SomeMatchingComponent.touch();
Result res = ensemble.match(args[0], args[1]);
}
}
Notice the commented touch() call, I will get back to this later.
public class Ensemble{
private List<MatchingComponentInterface> components;
private Result combinedResult = new Result();
public void addComponent(MatchingComponentInterface component){
components.add(component);
}
public void match(Object first, Object second){
components.forEach(component -> {
combinedResult.addResult(component.match(first, second));
});
}
}
Furthermore I might have several MatchingComponent implementations looking roughly like this:
public class SomeMatchingComponent implements MatchingComponentInterface{
//this is only executed the first time the class is mentioned in the code
//however I want this do be executed without mentioning the class
static {
MatchingComponent foo = new MatchingComponent();
Main.getEnsemble().addComponent(foo);
}
//static void touch(){}
public Result match(Object first, Object second){
//matching magic
}
}
Now take a look at the static code bock. This code is executed as soon as I use the class somewhere else in the code. However in this example this won't happen as I commented out the touch()
method as well as the call in the main method.
When the ensemble is built the main method needs to know all components beforehand in order to to touch and add them to the ensemble. However I want to add them without any of that. They should add themselves.
My question now is: Is there a way to force execute the static code block anyway without hard coding which components exist or maybe let the interface call all implementations of itself?