The usual answer here is to create a new class which has properties for these things, and pass around an instance of that class (sort of as a message envelope) rather than a bunch of discrete arguments.
For example:
public void create(MyNiftyThing thing){
object.create(thing);
}
...where the MyNiftyThing
class is:
class MyNiftyThing {
private String x;
private String y;
private String z;
private Calendar a;
private Calendar b;
private Calendar c;
private List<String> d;
public MyNiftyThing(String x, String y, String z, Calendar a, Calendar b, Calendar c, List<String> d) {
// ...init data members here...
}
// ...accessors for the data members here...
}
You can keep it immutable by only having getters and not setters. Sometimes, if it's purely a convenience data structure, you might use publics for the instance members, but any decent IDE can auto-generate the accessors for you (and if you used publics, you'd be stuck with final
for immutability).
Of course, you don't have to accept all of those in the constructor (or you can have multiple constructors), if there are reasonable defaults you can supply.
It probably doesn't apply to this specific case, but for instance MyNiftyThing
can be its own builder by having a constructor accepting minimal args (possibly none) and then a series of buildNNN
methods that set their relevant data member and return this
for chaining. That can help avoid getting lost in the constructor signature ("Was it x
, y
, z
or x
, z
, y
...?"). :-) The downside to that is that you essentially lose immutability.