0

In a given class, I have either .methodA(); or .methodA(param); It's not just one method, there are about 20 .method_();'s.

So, I don't want to use overloading, as that would be ~30% code bloat.

I've found a few relevant questions but some of them are about a number of parameters from 1-n... And I have method calls with no parameters. Other questions just say I can't and need to use overloading, but these questions are several years old - perhaps the Java gods have been generous since then?

(Java optional parameters)

DraxDomax
  • 1,008
  • 1
  • 9
  • 28
  • Can you show some code to illustrate the problem? – Sweeper Nov 27 '19 at 10:17
  • 1
    Varargs also work with 0 parameters, for example `methodA(String... strings)` will also accept 0 parameters - not just 1-n parameters. – Jesper Nov 27 '19 at 10:19
  • Without a code example is difficult to undrestand your question. – SuperG280 Nov 27 '19 at 10:21
  • I am a big fan of showing work before getting free advice but, in this case, I've already shown all the code that is logically needed to understand the problem. I mean, I can give an example of method overloading but that would hardly be of use to anyone. – DraxDomax Nov 27 '19 at 10:32

2 Answers2

0
public void method(Object ...args);
HMD
  • 2,202
  • 6
  • 24
  • 37
joe
  • 1
  • How do you call this "..." syntax? When was that introduced? – DraxDomax Nov 27 '19 at 10:35
  • 1
    @DraxDomax It's called Variable Argument (Varargs), and were introduced in Java 5. – Ctorres Nov 27 '19 at 10:40
  • Joe, I really want to accept your answer, could you please take a look on whether the expression should be "Object ...args" or "object... args". I've used the latter in my code (to great success) but not sure which is the correct syntax – DraxDomax Dec 05 '19 at 14:25
0

Sometimes one sees a Map being passed; primitive. But considering you are talking about possible 20 variations of parameters, something like properties, a Map, that is not really heavily processed, might be okay. This can even be passed as a fluent api of key-value pairs. Since java 9:

obj.method(Map.of("a", 1,
                  "b", 2,
                  "c", 3));

There is one solution that that is quite verbse, storing all parameters in its own class, but would allow integrity constraints and checks, such as either pass A or otherwise pass B and C, setting A to f(B, C).

You can use a builder pattern. It comes with an object creation at the end with fields containing all possible parameters.

There are several variations in using builders. The most applicable for this case would be:

void method(Param param) { ... }

public class Param {

    public final String s;
    public final int x;
    public final int y;

    private Param(String s, int x, int y) {
        this.s = s;
        this.x = x;
        this.y = y;
    }

    public static ParamBuilder newParam() { // Possibly obligatary parameters.
        return new ParamBuilder(); 
    }

Which ensures that one uses a special build to begin building the Param.

    public static class ParamBuilder {
        private String s;
        private int x;
        private int y;

        private ParamBuilder() { };

        public ParamBuilder withS(String s) {
            ...; return this;
        }

        public ParamBuilder withXY(int x, int y) {
            ...; return this;
        }

        public Param build() {
            // Possible integrity checks for completeness.
            return new Param(s, x,  y);
        }
    }
}

Usage:

object.method(Param.newParam().withXY(3, 4).withZ(5).build());
object.pMethod().withXY(3, 4).withZ(5)).call();
//        |                              |
//      newParam                       method(build())
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138