Reputation: 10607
I'm creating a Java class that should encapsulate the six orbital elements of a celestial object, the six osculating elements of the same celestial object, the mass of the body and the name of the body. This means that my Java object must be created with no less than fourteen parameters, and I am now thinking about including another four constants of perturbation as parameters, which will bring that number up to eighteen.
This is how it looks with fourteen parameters:
new Planet("Mercury", 3.3022E23,0.387098, 0., 0.205637, 0.00002123, 7.00559, -0.00590158, 252.252, 149473., 77.4577, 0.1594, 48.3396, -0.122142)
I've looked around people say that a class that takes in more than ten parameters is probably poorly designed. They also say that a class should do one thing and one thing only. Well, I'm just doing one thing literally, the only thing the class does so far is calculating the position of the celestial object with those parameters as a function of time.
What is best practice for dealing with this situation?
Upvotes: 3
Views: 2068
Reputation: 2285
I would recommend you to use Builder Design Pattern. If you are using lombok annotation all the verbose code can be generated by using @Builder annotation.
@Builder
class Some{
private String a;
private String c;
private String d;
private String e;
}
You can generate object with the following semantics:
Some someObject = Some.builder()
.a("a")
.b("b)
.c("c")
.d("d")
.e("e)
.build();
Upvotes: 0
Reputation: 20163
I recommend the Bloch Builder, by Joshua Bloch (item 2) in Effective Java, 2nd edition:
http://www.informit.com/articles/article.aspx?p=1216151&seqNum=2
It is a pattern designed specifically for classes with lots of fields, although it is intended for optional parameters, which is not your case. However, I still think this might be a good way for you to approach it. Such as
Planet p = new Planet.Builder("Mercury").gravity(3.3022E23).
anotherAttribute(0.387098).avgTemp(0.).
somethingElse(0.205637).andAnotherThing(0.00002123).
....
build();
(change them to meaningful stuff...I have no idea what the numbers actually represent :)
I recommend against setters in the Planet
object, in order to make the fields immutable ( https://www.google.com/search?q=fields+immutable+java+benefit).
I hope this helps.
Upvotes: 3
Reputation: 18163
I would prefer combining the already mentioned solutions - as you write in your intro "I'm creating a Java class that should encapsulate the six orbital elements of a celestial object, the six osculating elements of the same element, the mass of the body and the name of the body.", it seems to me that you can group each six parameters into a new datastructure, so that you end up with four parameters for the Planet
constructor (name, mass and the two parameter objects with six own values each) - next step I would ask myself if the six orbital and osculating elements somehow carry extra meaning or are merely a group of six (as in "arbitrary number") elements and can therefore be represented as a list.
Upvotes: 1
Reputation: 1215
I would just have a bunch of setters. Maybe use name as constructor parameter. Just to make it clearer to read. Figuring out which of those 14+ parameters is which is just too difficult for the reader if you set them all in the constructor. Or use a builder as suggested by others.. Both are about the same for me.
Upvotes: 1