Java – How to design extension mechanism for java app

designdesign-patternsenterprise-architectureextensibilityjava

I have an application where I have a set of core features.

  1. The application itself HAS to work in core only mode.

  2. I would also want to make the application extensible such that, when certain jar/plugin files exist in the classpath/installation location, additional features would be available, things would just be there. This is OPTIONAL.

The way I would do it is using the factory pattern with a modification.
In the core package, I have an extension point (implemented by either a public static variable or a singleton, or other extension point model from my framework – Eclipse RCP). The extension jars/plugins get this extension point and registers its factory object to this extension point. (This is best done with RCP plugin/feature activation mechanism, or it can just be anything that causes a load of the extension jar/plugin factory class)

When the application runs, the core factory would produce objects from the extension factory if one has been registered or use its core factory otherwise.
This is how the application goes from core mode to extension mode if an extension jar exists.

I wonder if this is a decent way to do what I wanted to do or this is just a hack, anti-pattern, etc…
Please propose a better solution if you can. Thanks.

Best Answer

Java has a standard mechanism precisely for this, the java service providers, thinks in the way you use a JDBC plugin, you put the JDBC driver in your classpath and your application now can connect to this concrete database provider (oracle, mysql). In fact this is used in a lot of java standards that define an interface an allow different implementations.

Its not very different from you implementation, but its a standard and there are some classes in the jdk to make this easy to implement, for example take a look to this: http://docs.oracle.com/javase/7/docs/api/java/util/ServiceLoader.html

Related Topic