4

For routing action requests, I'd like to use a single ActionListener instance for all Components. Calls on actionPerformed could be differentiated using setActionCommand.

Is this advised ? Are there any potential downsides to this approach in an MVC context ?

James P.
  • 19,313
  • 27
  • 97
  • 155

3 Answers3

6

MVC only dictates to split M, V and C. It does not mandate some kind of cardinality for the relationships though. So you're free to go.

If the switch logic inside the event-handling method gets too complex (like more than a few lines), you might want to think about splitting the one huge class into multiple classes. Provides better overview and maintenance. Believe me: if you get back to a simple Listener to change it, you will be really happy. This would be a Refactoring then; your IDE might provide according support.

So my advise: KISS - do it simple first an put your stuff in one Listener. If it gets too complex: make it simple again and refactor to multiple Listeners (that would be another application of KISS).

Just remember to keep your code simple and don't be lazy creating classes. Creating a class is way more simple than getting your head back into complex code. Always remember: creating software is 20%, maintenance is 80%. Make the code simple to understand now and maintenance will be simple later. The opposite applies either.

ChriWeis
  • 207
  • 2
  • 7
5

"Is this advised ?" I'd imagine not, but a lot will depend on the size of you project.

I think the statement "all your eggs in one basket" comes to mind.

The size of the if statement could become unimaginably huge as you add new action branches to it. As you're project increases in complexity, so will you action handler.

Debugging potential problems could also become a headache.

Maintenance of the code would become tedious and prone to misunderstandings and cause no end of new errors in the logic.

You break the isolation of responsibility rule (you should try and isolate responsibility for the management of an action/event within it's own object/class)

If you're looking for a way to re-use action handlers, look at the Action API instead. If you're really eager, you could use a Factory to produce well know actions through out your code. This would allow for re-use while maintaining responsibility isolation.

On a personal note, having had to spend the last 3 years unraveling the mess of a inexperience graduate who wrote the core libraries to our system and liked to code like this, please don't. It's so easy to break, so hard to read and is just a plain mess.

IMHO it speaks of a lack of/or inability to design.

That's just my personal opinion based on my experience

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • Thank you for your insights. What I want to avoid is having a series of set methods for each Component in the view. If the controller acts a Mediator and sets up the view, how would you suggest Actions be used ? Would there be a way of routing calls according to String values to decouple things a bit ? I have been thinking of having some sort of action registry. – James P. Aug 10 '12 at 05:35
  • 2
    It's a question of effort over time. Personally, the effort involved in maintaining a centralised event handler of this nature isn't worth the time. Much easier to simply create small units of work that already integrated into the API (see `JMenu.setAction()`, `JMenu.add(Action)`, `AbstractButton.setAction()`). A centralised registry isn't a bad thing and again comes down to the complexity and size of you your application, you just need to come up with a reasonable name look convention. – MadProgrammer Aug 10 '12 at 05:52
2
  • most complex way is add EventHandler, which fired event, this/these event(s) could be compared in String value

  • advantage I don't know any way (forgot about Reflection) how to pass String value as parameter through Java Classes

  • disadvantage (my view) required deepest knowledge, the same in compare with Reflection

  • next best choices could be mentioned Swing Action, is great and scallable, have to check methods starts with isXxx

  • for MVC is better and correct way to build code and to distribute events through Swing methods by using ProprertyChangeXxx

  • for JTextComponents to use TextAction rather than ProprertyChangeXxx (maybe I'm wrong, maybe wrong concept, nothing give me the better alternative)

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319