In my one class I have many constructors like this..
public MyData(int position,String songName,String duration, boolean e) {
//initialization of above variable like int, string,string and boolean
}
public MyData(String songName, String artistName, String duration,String downloadPath, String songSize, String albumName,String url,String trackId, boolean e)
{
//initialization of above variable like String,String,String,String,String,String,String,String and boolean
}
and some more like above.
Now the calling time, I’m calling that constructor only that I require data. but I don’t think my flow is good so I need some help to reduce my code as well as creation of good flow.
If anybody have a good flow to achieve this, then please share.
Thanks in advance.
Assuming you’re effectively applying defaults, usually the best approach is to have one “full” constructor and make the others call it. For example:
You still end up with quite a lot of cruft in terms of overloaded constructors, but at least each of those “extra” constructors contains no actual code – just a call to another constructor. If possible, chain the constructors together so that the default for any particular value is only specified in one place – or use a constant. That way you get consistency.
Another option is to use a “parameter object” following the builder pattern – create another class whose sole purpose is to hold the data for the constructor parameters. This should be mutable, with setters for all of the different values. Often it’s useful to make the setters return the builder, so you can use:
This is particularly useful if the main type you’re constructing is an immutable type – it means you can apply conditional logic in the calling code to set some parameters but not others. The Java framework itself uses this approach in
ProcessBuilder, although personally I’m not keen on the way it overloads method names to either return a value or set a value based on whether you provide an argument 🙁Note the comment above the constructor call in the final snippet – if your helper class is only ever helpful for creating objects of a single type, you can give it an extra method (
build,create,start, whatever is most appropriate) to take the place of the constructor call. This allows you to build the whole final object in a fluent way.One option in the Java implementation of the builder pattern is to use a nested type, e.g.
That avoids polluting your package with another class which is only useful for building instances of
Foo.