Java program using plugin type structure, need feedback on proper design pattern

286 views Asked by At

Sorry if this question has been asked/answered before, but I was unsure how to formulate it properly, and thus my searches may have been improperly worded.

I am building a Paint application in Java as an assignment. I have decided to do this using a "plugin" type approach. The main Application offers very little in terms of functionality, but does dynamic class loading and registers the different plugins and such. Essentially, this allows me to add different components, without having to hard-code anything into the main program: a LoadComponents() method exists which finds all the different plugins within a sub-package and loads them. This works perfectly.

Each component is then in charge of creating and registering it's UI elements with the core application's JFrame, as well as implementing its own EventListeners. All of this works beautifully so far.

Here's my issue: I have a JPanel that I use as a Canvas, which obviously implements the paintComponent() method in order to do the actual painting. This is contained within the core application, and thus offers very meager functionality. As different components get added, I would like to add more and more functionality to the base Canvas class, but I can't seem to find a good, clean and effective way to do so.

For the time being, all I can think of is to make subclasses of Canvas that inherit it's functionality. However, in order to do so using the current approach where plugins and functionality is dynamically loaded at runtime, I need to offer a setCanvas() method or such that allows me to override the base Canvas class reference in the actual UI, which, to me, isn't very effective nor common practice.

There's also the problem of multiple plugins adding functionality to the base Canvas class, and then figuring out which should go first.

Any ideas, suggestions or comments on the matter would be greatly appreciated.

1

There are 1 answers

1
jzd On

I think the term "Plug-in" is either not exactly what you are doing or it is not the best design path for this application.

I believe what you want to possibly a Command Pattern where UI components send "Commands" to the "Canvas" which draws (and keeps repainting) each command. So each painting UI item would have to be able to create commands but the "Canvas" does not have to know where it came from.