More general return

Looking at some code cleanup, and I was wondering how best to handle this:

A class with some private variables, for example:

myBool1, myBool2, myBool3 myInt1, myInt2, myInt3 myString1, myString2, myString3 

What is the best way to execute a getter function that is common to the return value? Therefore, if I call a getter with something like:

 myNewBool=<blah>.get("myBool1") myNewString=<blah>.get("myString2") myNewInt=<blah>.get("myInt3") 

Anyone have any suggestions?

+1
source share
4 answers

You cannot have a common recipient unless you know what you want to receive, for example:

 boolean myNewBool= get("myString1"); 

If get returns something, but you really don't know if it is Boolean compatible, and terrible things can happen.

You can try the following:

 public <T> get(String element){ return (T) elementToGet; } 

But you will need to specify the return type when you call the get method.

 String element = myObject.<String>get("element"); 

Here are the bad sides:

  • You cannot directly work with primitives
  • You may have a lot of ClassCastException
  • If you missed the attribute name, you won’t see it until you run it.
  • You are not publishing a good public API, people should know that it can use the evert attribute to use it, and, as mentioned above, the attribute with an error (or unstable) will not be displayed until executed.
  • You must know the return time and enter it each time you use your method.
  • You will need to enter a very long (and smelly) code in your get method, either to use every possible attribute (if you still want to have some private and inaccessible ones), or worse, use reflection to find the correct attribute.

So, finally, not a good idea .

Instead, you can use the good old getters // seters and, if there are a lot of them, generate them using the IDE.

Another way would be to use the lombok project.


Resources:

In the same topic:

+6
source

First you must ask what the pros and cons of such a decision will be.

Pros:

  • One method instead of many

Minuses:

  • Unintuitive for users of your class (classic getters are more common)
  • You cannot have an overload that differs only in the type of return, so you have to have methods like getBool, getInt, etc.
  • This is slower - you need to pass the string, check the validity, perform a search on the map ...

The only advantage of your proposed solution is not to repeat the get () / set () code. However, since these methods are usually generated by your IDE and contain only a single-line command, I would not notice that this is a big problem.

To answer your real question - you can create a HashMap with matching attribute names. Alternatively, you can use Java reflection to access attributes. The second solution is more general, but harder to write.

+3
source

This is a really terrible idea. I'm not sure why creating a getter / setter for each private variable is a problem, but going around lines that map to the symbolic name of the variable will be difficult to maintain and confuse. You do not need this to be common; each variable represents a different amount, and should be referred to as such.

+1
source

It will not be clean, but a mess. I would either create 3 getter methods for the fields, or I would completely rework it. But calling the function with the field name to return, because the argument cannot bring anything good.

When you enter the code, you must constantly update your code for the entire encoding time. But not like that. The solution is to delegate logic to another class, transfer code to more useful methods, or change and simplify domain objects ...

0
source

All Articles