0

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 ?

ArkDeus
  • 143
  • 9
  • Doesn't equal type parameters mean they're compatible? i.e. and ```Algorithm``` is always compatible with and ```Exporter```. Or do you mean that the type ```Labyrinth``` might be unknown? – Jorn Vernee Jun 07 '16 at 15:28
  • As a side note, the ```Executor``` class has 3 type parameters, while it only really needs 1. – Jorn Vernee Jun 07 '16 at 15:29
  • 1
    `Algorithm` is a producer, `Exporter` is a consumer. `Executor` can simply have references to a `Algorithm extends T>` and a `Exporter super T>`. (I guess this is what you are suggesting, @JornVernee?) – Andy Turner Jun 07 '16 at 15:30
  • I edited my question, I wasn't clear enough. Can you explain a bit more your second answer please ? – ArkDeus Jun 07 '16 at 15:31
  • You could just declare ```Executor``` and replace occurrences of ```A``` with ```Algorithm```, ```E``` idem. Or like Andy suggested ```Algorithm extends T>``` and ```Exporter super T>``` which gives a little more flexibility. The only thing the parameters really do is restrict what types you can input in the constructor. – Jorn Vernee Jun 07 '16 at 15:35
  • Ok thanks, that's more efficient that way. I'm changing it. I won't be able to instantiate an executor if there is an incompatibility, but no way to discriminate the services ? – ArkDeus Jun 07 '16 at 15:39
  • They're discriminated by type are they not? If I code an ```Algorithm``` and someone else codes an ```Exporter```, I can not make an executor with those 2. – Jorn Vernee Jun 07 '16 at 15:48
  • My bad, I was using a raw type in my little test, that's why it was possible to instantiate it. Even with differents generics. – ArkDeus Jun 07 '16 at 15:56
  • Unfortunately, `ServiceLoader` does not consider generics for loaded classes. You'll need to check yourself that type parameters of the `Algorithm` and `Exporter` do match. – Konstantin Pavlov Jun 28 '16 at 08:21

0 Answers0