This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

multiple action listeners .setActionCommand Vs. addActionListener(new comboListListener())  RSS feed

 
bob von ilten
Greenhorn
Posts: 21
Linux Mac OS X MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All

I have been working on a form that will have multiple buttons and comboboxes. I have worked out two ways to achieve the functionality of multiple listeners for multiple sources, or one listener per source might be a better way of putting it. And I am wondering about the performance / memory usage of each method.

My question is a multipart question.

which method will use more resources as the number of sources grows?

would there be a performance hit for either method?

would either method present a security hazard?

does method one seriously break OOP?

TIA

Method One: use one common listener and the setActionCommand differentiate between sources.



Method Two: use actionListener classes for each object that needs to be heard

 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bob von ilten wrote:I have been working on a form that will have multiple buttons and comboboxes. I have worked out two ways to achieve the functionality of multiple listeners for multiple sources, or one listener per source might be a better way of putting it. And I am wondering about the performance / memory usage of each method.

This sounds to me like a very specific question; so, if you don't mind, I'm going to move it to the relevant forum.

However, I would say this: Don't worry about performance until you KNOW it's an issue; concentrate instead on getting the design right. Chances are, the latter will get you close to the first anyway.

Winston
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use method 2, it is the better design. The creation of one extra class will have no affect that you will be able to measure.

As a general rule, the only time you should use the action command is to handle common functionality. For example maybe you are creating a calculation and you want buttons to enter the digits 0-9. So in this case you can use a common ActionListener and then just use the getActionCommand() method to determine which digit to add to your calculator display. This type of code doesn't require nested if/else or case statements.

 
bob von ilten
Greenhorn
Posts: 21
Linux Mac OS X MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks I appreciate the feed back. I will move forward with the individual class design. As to moving it that is fine with me, the main reason I posted it in the beginning forums was that is where I seem to be living lately :-) I also had a rough time finding a simple code example of dealing with multiple sources and single listeners per source. I thought the code example might help some other beginners.

But either way - Thanks Again
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!