• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 351
  • Last Modified:

Best practice for handling events for multiple gui components

What is the best practice way to handle events, when multiple event sources (e.g. multiple buttons) are present in a gui? Should separate listeners be created or only one with a switch defining the source of the event? Or something else?
0
Suppai
Asked:
Suppai
  • 2
2 Solutions
 
CEHJCommented:
I don't think there's one answer to that. At least there shouldn't be. It would depend on your requirement. Looking at a short 'switch' handler makes things easier to maintain, but it's less 'OO'. A very long 'switch' gets unmaintable. Many separate handlers are more OO but more difficult to grok for a new maintainer
0
 
objectsCommented:
separate listeners, avoid using switch at all cost. Use of a switch generally means poor design.
0
 
SuppaiAuthor Commented:
Can you provide an example of good design using separate listeners then?
0
 
objectsCommented:
sendButton.addActionListener(new ActionListener()
{
   public void actionPerformed(ActionEvent event)
   {
       send();
   }
});
doSomethingElseButton.addActionListener(new ActionListener()
{
   public void actionPerformed(ActionEvent event)
   {
       doSomethingElse();
   }
});

0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now