Example

This example show a simple interface that I created in the Flash IDE, which contains a list of different modules to be loaded at runtime. To run the example, select a Module from the list, then click Load Module. When you do this, the shell will load the selected module and the numbers will be feed into the module and a result will be returned by the module. In the result box, you should see the result.

You can enter different numbers in the input fields (don’t try invalid values like letter ‘a’ please) and press Calculate to see the result box changes.

The example does no do much but it illustrates how the principle can be applied for plug-ins or dynamic binding during run-time.

Please upgrade your browser

Remember that in this example, you need to press Load Module after you select the module, otherwise the result field will not be updated.

The mModuleURL is used for unloading the module. This is not strictly necessary and there are other creative ways to do this. getModuleName() returns the class name of the module, again not required, just for a demo purpose. performOperation() is a stub method for the other class (in a real application, you’d likely to create an Interface class for this). The three classes are shown below:

You can see all they do are overriding the
performOperation() function to do different things (addition, subtraction, multiplication). In real applications, they can be image processing operations, complex calculus, graph rendering, and so on.

The Shell UI

This Shell.FLA is a SWF that I created in the IDE for the purpose of creating some sort of User Interface. Nothing here is strictly required to use the module as the FLA only contains standard Flash UI or Components. The large white box is a TextArea that acts as a console for debugging purpose.

The Shell class

This class is the one responsible to load a module, unload a module, and call functions within the module. Although in this example, there is only one module active per given time, there’s no technical limitation that say so.

You can examine the shell class in the download package (linked at the bottom of this document), but here’s the part that load the module through a ModuleManager:

The Module Manager

This simple manager handles the task of loading modules and when the module is loaded, it redispatches the ModuleEvent.READY event. In this case the event is then intercepted by the shell and the shell creates an instance of the module (if there’s not already one in the manager). You can improve the class by having a cache of frequently used modules.

The mModuleList keeps a list of the modules (or rather, the moduleInfo) and its instances (in moduleInstance) that are already loaded and instantiated. This means that modules are cached and can be re-used without having to reload again. Note: in this example, you can only have one instance per module, so if you need to have multiple instances, then this won’t work.