how to reduce the code of constructor overloading

2019-07-29 23:47发布

问题:

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.

回答1:

Assuming you're effectively applying defaults, usually the best approach is to have one "full" constructor and make the others call it. For example:

public Foo(String name)
{
    // Default the description to null
    this(name, null);
}

public Foo(String name, String description)
{
    this.name = name;
    this.description = description;
}

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:

FooParameters parameters = new FooParameters()
    .setName("some name")
    .setDescription("some description");

// Either a constructor call at the end, or give FooParameters
// a build() or create() method
Foo foo = new Foo(parameters);

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.

Foo foo = new Foo.Builder().setName(...).setDescription(...).build();

That avoids polluting your package with another class which is only useful for building instances of Foo.



回答2:

You may want to have another object that is responsible for creating the object through the builder pattern. For example, you could define an object like this:

public class SongBuilder {
    private String artistName;
    private String songTitle;
    /* ... everything else ... */

    public SongBuilder setArtistName(String name) {
        this.artistName = name;
        return this;
    }
    public SongBuilder setSongTitle(String title) {
        this.songTitle = title;
        return this;
    }
    /* ... everything else ... */

    public Song create() {
         return new Song(artistName, songTitle, /* ... everything else ... */);
    }
}

You could then define a single constructor for Song that takes in all the data. To make a Song, you could then write

 Song s = new SongBuilder().setSongTitle("Still Alive").setArtistName("GLaDOS").create();

The advantage of this approach is that you can set a reasonable default for all the parameters, then just call the appropriate set functions for parameters that you actually use. It also allows you to add new parameters easily without having to go back and rewrite important code.

Alternatively, as Jon Skeet points out, you can have multiple constructors that all call one another. The advantage of the builder pattern over this approach is that if you have n different parameters, there are 2n combinations of constructors you'd need to write, whereas you only need one builder.

Hope this helps!