3

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.

  • I have some doubts as how would you implement the discovery process (to find which classes are available). Also, while I do not find your initial proposal bad, maybe you should check if you can use Context and Dependency Injection (CDI) – SJuan76 Jul 2 '14 at 17:59
  • @ SJuan76 The extension jar has to import the core jar so that it can have access to the core's extension point. Now that it has access to the core's extension point, it simply creates and passes its factory object (which creates extended objects) to the core. This happens during app init. Now the core just uses the extended factory. There is no other discovery process. – InformedA Jul 2 '14 at 18:05
  • Ahhh, I understand what you mean by the discovery process now. I would use either RCP framework's plugin/feature activation or Java's ServiceLoader. – InformedA Jul 3 '14 at 4:29
  • You might want to use or define some class loader. – Basile Starynkevitch Jul 3 '14 at 8:14
  • Seems OSGI is what you are looking for. – xeranic Jul 25 '14 at 20:35
2

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.