0

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?

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
user1406177
  • 1,328
  • 2
  • 22
  • 36
  • 3
    umm. that sounds like basic design work , and that is a problem you really have to solve. it really depends on what your requirements are. – Markus Mikkolainen Jul 30 '12 at 21:28

1 Answers1

0

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:

public interface IPlugin {
    // some methods that all the plugins will have in common
}

private static IPlugin loadIPluginByClassName(String plugInClassName, ClassLoader classLoader) {
    // NOTE: throws NoClassDefFoundError or ClassNotFoundException if cannot load the class
    // or throws ClassCastException if the loaded class does not implement the interface    
    Class<?> cls = classLoader.loadClass(plugInClassName);
    IPlugin ans = (IPlugin) cls.newInstance()
    return ans;
}

You'll need to call loadIPluginByClassName() with a package qualified class name and class loader, as in

IPlugin pi = loadIPluginByClassName("com.test.something.MyPlugin", SomeClassInYourProject.class.getClassLoader());

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

IPlugin pi = loadIPluginByClassName(MyPlugin.class.getName(), SomeClassInYourProject.class.getClassLoader());

this creates a direct reference to your plugin class (which can be helpful in that such can be found as a reference)

Community
  • 1
  • 1
Erik Eidt
  • 23,049
  • 2
  • 29
  • 53