I have an api which has some base implementations. I have a factory which gives the instances of that api to the clients.
I want to change my factory to make it more generic so, if a new implementation of the api will be generated and its jar file will be put in classpath, the factory will understand it and any changes wouldn't be needed.
Use the java SPI, Service Provider Interface.
META-INF/services/my.package.MyInterface
one lists implementing class(es).The service discovery happens with a
ServiceLoader<T>
:You could provide a class in the API jar with a static function for that discovery mechanism.
Advantages:
Example of jars
my.package.impl.MyImpl1
public class MyImpl1 implements MyInterface { ... }