We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Best practice for handling events for multiple gui components

Suppai
Suppai asked
on
Medium Priority
359 Views
Last Modified: 2013-11-23
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?
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016
Commented:
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

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
separate listeners, avoid using switch at all cost. Use of a switch generally means poor design.

Author

Commented:
Can you provide an example of good design using separate listeners then?
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
sendButton.addActionListener(new ActionListener()
{
   public void actionPerformed(ActionEvent event)
   {
       send();
   }
});
doSomethingElseButton.addActionListener(new ActionListener()
{
   public void actionPerformed(ActionEvent event)
   {
       doSomethingElse();
   }
});

Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.