java构造函数中的许多参数

时间:2018-04-12 10:04:07

标签: java constructor

嗨,我是java的初学者。

在我的BookLibrary程序中创建具有许多参数的构造函数是否是一种好方法?

public class Book implements Serializable {

private String title;
private String directorName;
private String directorSurname;
private String type;
private int issueYear;
private List<String> actorNames;
private List<String> actorSurnames;
private Tuple<String, String> directorFullName;


public Book(String title, String directorName, String directorSurname, String type, int issueYear,
            List<String> actorNames, List<String> actorSurnames, Tuple<String, String> directorFullName){
    this.title = title;
    this.directorName = directorName;
    this.directorSurname = directorSurname;
    this.type = type;
    this.issueYear = issueYear;
    this.actorNames = actorNames;
    this.actorSurnames = actorSurnames;
    this.directorFullName = directorFullName;
}

或者有更好的想法来创建这样的构造函数吗?

2 个答案:

答案 0 :(得分:2)

正如其他人已在评论中所说,使用Builder Pattern是一种选择。但如果处理不当,则会带来创建不完整对象的风险。

但是有更多方法可以改善您的设计。例如。你传递名字和姓氏(以及导演的全名 - 为什么只有?)作为单独的字符串。我创建了一个PersonName类来封装这些不同的命名元素,因此构造函数变为:

public Book(String title, 
            PersonName directorName, 
            String type, 
            int issueYear,
            List<PersonName> actors) {
    ...
}

看起来更好,并使命名问题更加一致。

当然,将该类重命名为Movie而不是Book

答案 1 :(得分:1)

创建一个包含3个以上参数的构造函数并不是最佳实践。因为您需要知道每个参数的顺序。我建议你以这种方式使用getter和setter(对于 title 类的字段):

public String getTitle() {
    return title;
}

public Book setTitle(String title) {
    this.title = title;
    return this;
}

通过这种结构,您可以在创建新实例时创建非常漂亮的结构:

Book book = new Book()
            .setTitle("Book")
            .setType("Comedy")
            .setActorNames(Arrays.asList("Abzal"));

您班级的完整重构版本:

public class Book implements Serializable {
private String title;
private String directorName;
private String directorSurname;
private String type;
private int issueYear;
private List<String> actorNames;
private List<String> actorSurnames;
private Tuple<String, String> directorFullName;

public Book() {
}


public String getTitle() {
    return title;
}

public Book setTitle(String title) {
    this.title = title;
    return this;
}

public String getDirectorName() {
    return directorName;
}

public Book setDirectorName(String directorName) {
    this.directorName = directorName;
    return this;
}

public String getDirectorSurname() {
    return directorSurname;
}

public Book setDirectorSurname(String directorSurname) {
    this.directorSurname = directorSurname;
    return this;
}

public String getType() {
    return type;
}

public Book setType(String type) {
    this.type = type;
    return this;
}

public int getIssueYear() {
    return issueYear;
}

public Book setIssueYear(int issueYear) {
    this.issueYear = issueYear;
    return this;
}

public List<String> getActorNames() {
    return actorNames;
}

public Book setActorNames(List<String> actorNames) {
    this.actorNames = actorNames;
    return this;
}

public List<String> getActorSurnames() {
    return actorSurnames;
}

public Book setActorSurnames(List<String> actorSurnames) {
    this.actorSurnames = actorSurnames;
    return this;
}

public Tuple<String, String> getDirectorFullName() {
    return directorFullName;
}

public Book setDirectorFullName(Tuple<String, String> directorFullName) {
    this.directorFullName = directorFullName;
    return this;
}

}

有一个好的编码!