My application use ServiceLoader, and I have two types of services : Algorithm and Exporter. The code :
public abstract class Algorithm<E> {
public abstract E process();
public abstract void askParameters();
}
public abstract class Exporter<E> {
public abstract void export(E element);
public abstract void askParameters();
}
I have another class, Executor, which "wires" the algo and the exporter together :
public class Executor<T, A extends Algorithm<T>, E extends Exporter<T>> {
A algo;
E exporter;
public Executor(A a, E e){
algo = a;
exporter = e;
}
public void execute(){
algo.askParameters();
exporter.askParameters();
exporter.export(algo.process());
}
}
Team members independentely code algorithms and exporters for several types of things. They put it in a jar, register service providers that are either Algorithm or Exporter, and the core of the application consists in combining those modules.
I wrote that little test :
public class MainTestServiceLoader {
public static void main(String[] args) {
AlgorithmService algoService = new AlgorithmService();
ExporterService exporterService = new ExporterService();
Algorithm<Labyrinth> braidMaze = algoService.getAlgorithm1();
Exporter<Labyrinth> toLvl = exporterService.getExporter1();
Executor<Labyrinth,Algorithm<Labyrinth>,Exporter<Labyrinth>> executor = new Executor(braidMaze,toLvl);
executor.execute();
}
}
But I foresee problems ahead. Since the loader doesn't know the specific type of an Algorithm or an Exporter, how can I be sure that the combination of the two will be compatibles ? Or is there a way to filter the services in the service loader depending of what type the algo/export works on ? Thanks you
EDIT : in order to be more clear : let's say someone code a module Algorithm, and the other code an Exporter. They will be loaded the same way in the ServiceLoader. Is there a way to discriminate them ?