I am working on a project that connects to different devices of the same type. I have implemented a few classes for single devices, but now developed a generic interface that all “drivers” should implement.
My Problem is now: Later, the user should use my program via GUI and should be able to “load” the drivers offered from me. But how do these drivers look like? A .jar with a class that implements the driver interface? A xml file that describes roughly what to do?
You might consider the following post as a starting point: https://stackoverflow.com/a/2575156/471129
Basically, dependency injection and inversion of control are used to externally specify configuration of and dynamically load code often from different .jar files, which seems like what you’re trying to do.
Of course, you can always load code by class name with something like this:
You’ll need to call loadIPluginByClassName() with a package qualified class name and class loader, as in
The plugin can be in its own .jar as long as that .jar is on the classpath.
If you don’t want to load them from an external .jar, you can use the simpler
this creates a direct reference to your plugin class (which can be helpful in that such can be found as a reference)