如何减少构造函数重载的代码 [英] how to reduce the code of constructor overloading

查看:159
本文介绍了如何减少构造函数重载的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个类,我有很多这样的构造函数。

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.

提前感谢。

推荐答案

假设你有效地应用默认值,通常最好的方法是有一个完整构造函数,并让其他人调用它。例如:

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;
}

在重载的构造函数中你还是有很多cruft,但是至少每个额外构造函数都不包含实际代码 - 只是调用另一个构造函数。如果可能,将构造函数链接在一起,以便任何特定值的默认值只在一个位置指定 - 或使用常量。

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.

另一个选择是在构建器模式之后使用参数对象 - 创建另一个类,其唯一的目的是保存构造函数的数据参数。这应该是可变的,为所有不同的值设置。通常,让setter返回构建器是有用的,因此您可以使用:

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);

这是特别有用的,如果你正在构造的主类型是一个不可变类型 - 在调用代码中应用条件逻辑来设置一些参数,而不是其他参数。 Java框架本身在 ProcessBuilder 中使用这种方法,虽然我个人不喜欢重载方法名称的方式,要么返回一个值,要么根据您是否提供一个参数:(

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 :(

请注意在最终片段中的构造函数调用之上的注释 - 如果你的帮助类只对创建单一类型的对象有帮助,给它一个额外的方法( build create start

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.

Java实现中的一个选项的构建器模式是使用嵌套类型,例如

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();

只有 可用于构建 Foo 的实例。

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

这篇关于如何减少构造函数重载的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆