Using Dependency Injection for GUI coding

Date:

Tags:

My colleague Jimmy and I spent a lunch the other day discussing client application GUI editors. We both agreed that the ones we know suck quite majorly since they create code can’t be touched, does not allow for any sort of creative programming etcetera. On the other hand, it is quite handy to see the GUI develop - not that the result always looks like the one drawn though.

So instead he made the case that you should have a window continuously rendering the code you’re writing. Sort of like seeing the GUI develop while you code instead of watching the code die while you draw the application GUI.

That seemed to require the GUI code to not do anything useful - be a good dumb window basically. The problem with almost all GUI code however is that listeners are implemented as anonymous inner classes, becoming impossible to mock out

Dependency Injection to the rescue! Have all listeners talking to external whatevers become parameters in the constructors. Then they can easily be mocked, the GUI code is clean and decoupled and the sun is shining. Now, it shouldn’t be too hard to have a Beanshell2 based Java code reading GUI-viewer, should it?

Since we’re not in the business of making client applications - anymore, that is - we haven’t tried it yet, but I’m eager to try it.