• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java 'Getters' - How to re-utilize them?  RSS feed

 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys! I am working on this project and I wanted to know if it was possible to re-use a getter, instead of having to create one for each return value (they are all of the type JPanel.



And this is a snippet from the class which is using this class:


My question is, instead of creating a huge number of getters and then remembering which getter I need, is there a way to re-use the same one? Like maybe I can add a parameter? Thanks!
 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin Scabbia wrote:Hey guys! I am working on this project and I wanted to know if it was possible to re-use a getter, instead of having to create one for each return value (they are all of the type JPanel.


Yes, you could accomplish that by passing a parameter (an enumerated type would work best) to the method and using a switch statement to return the panel associated with the enum value. From an object-oriented design perspective, though, the approach in code you already posted (where there's one accessor defined per panel) is preferable.
 
Benjamin Scabbia
Ranch Hand
Posts: 34
Eclipse IDE Python Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brett Spell wrote:
Benjamin Scabbia wrote:Hey guys! I am working on this project and I wanted to know if it was possible to re-use a getter, instead of having to create one for each return value (they are all of the type JPanel.


Yes, you could accomplish that by passing a parameter (an enumerated type would work best) to the method and using a switch statement to return the panel associated with the enum value. From an object-oriented design perspective, though, the approach in code you already posted (where there's one accessor defined per panel) is preferable.


Thanks Brett, after further research I think it will be the most effective way... although I could map my JPanels in a map with Strings as keys and they get them by name which provide the getter methods as arguments.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Benjamin Scabbia wrote:although I could map my JPanels in a map with Strings as keys and they get them by name which provide the getter methods as arguments.

If you do go this route, then as Brett said, you should use an enumerated type instead of Strings as keys - it reduces the chance of errors due to invalid strings being used as the parameter
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's wrong with the individual getters?
 
Campbell Ritchie
Marshal
Posts: 56598
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brett Spell wrote: . . .
Yes, you could accomplish that by passing a parameter (an enumerated type would work best) to the method and using a switch statement . . .
And calling your class Calendar? Look at its get() method.

That was one of the reasons everybody complained about the pre‑Java8 date‑time classes.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Look at its get() method.

Exactly why I asked by question. I think the OP is being led into the reeds.
 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Brett Spell wrote: . . .
Yes, you could accomplish that by passing a parameter (an enumerated type would work best) to the method and using a switch statement . . .
And calling your class Calendar? Look at its get() method.

That was one of the reasons everybody complained about the pre‑Java8 date‑time classes.


Huh?
 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:
Campbell Ritchie wrote:Look at its get() method.

Exactly why I asked by question. I think the OP is being led into the reeds.


Being led into which reeds and by who? Are we all talking about the same thread?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think that doing things like:

is an improvement over:

Also, of course, violating the bean conventions.
 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:I don't think that doing things like . . . is an improvement . . .


Neither do I, which is why I (and at least one other person) recommended against it, so I'm not sure why you suggested that someone (me?) was "leading him into the weeds".
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps worded too strongly, but this is not a direction that the OP should go.
 
Brett Spell
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Perhaps worded too strongly, but this is not a direction that the OP should go.


Again, preaching to the choir, since nobody has suggested otherwise and in fact just the opposite.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!