|
Build a Dynamic Module Subsystem
Make your programs more flexible with encapsulated modules
by Claude Duguay
May 2002 Issue
The ability to distribute modules in an encapsulated package that you can activate by simply dropping a module file into a specified directory is a powerful notion. This "plug-in" concept can make an architecture incredibly flexible, allowing you to add modules without needing to make any changes to the main program. It also makes it possible for users to get the modules they want with relative ease and to upgrade the software with minimal effort.
I'll show you how to develop a module architecture based on JAR files. To use it, you can define a common interface for modules of a given type and then package each concrete implementation of that interface in a separate JAR file. Using this infrastructure, your application can scan specified directories for module files and automatically make them available to your program through a factory mechanism.
To maximize flexibility, the ModuleFactory supports multiple module types. In other words, any number of instances for a given interface type can exist, but numerous interface types can also coexist in the same application.
When you want to retrieve an instance of your module type, you specify the interface it implements along with an arbitrary key Object that you can use to select the best module for a given task. Each module instance takes responsibility for deciding whether it can handle the type of Object you pass in as a key. The factory returns the first suitable match it finds.
This mechanism is incredibly powerful. You can use it to select suitable content handlers, protocols, filters, operations, transformations, program extensions, and more. Because the key is a Java Object, you can select a suitable module based on any criteria.
Back to top
|