0

Hi I am a quite beginner in java.

Is it a good way to create construcors with many parameters like in my BookLibrary program?

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

Or is there any better idea to create such a constructor?

Dawid
  • 53
  • 9
  • first check your logic. Books don't have directors and actors, so it is not logical to add any of those. – Stultuske Apr 12 '18 at 10:06
  • Have setters with each setters returning `this` and while setting the fields you can chain such setters. If there are some mandatory fields without which object has no meaning then you can use constructor for such fields and for rest have setters methods or you can also use Builder pattern – nits.kk Apr 12 '18 at 10:07
  • You should consider making use of the Builder Design Pattern in such case – piy26 Apr 12 '18 at 10:07

2 Answers2

2

As others already said in comments, using the Builder Pattern would be an option. But if not done properly, that introduces the risk of creating incomplete objects.

But there are more ways to improve your design. E.g. you pass names and surnames (and a full name in case of the director - why only there?) as separate Strings. I'd create a PersonName class that encapsulates these different naming elements, so your constructor becomes:

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

Looks better and makes naming issues more consistent.

And of course, rename that class to be Movie instead of Book.

Ralf Kleberhoff
  • 6,990
  • 1
  • 13
  • 7
  • you just repeat what the answers in the duplicate say. You could easily have checked because I flagged it 10 mins before you posted the answer. Please don't answer obvious duplicates – Tim Apr 12 '18 at 10:32
1

Creating a constructor with more than 3 parameters it is not a best practice. Because you need to know the order of each parameter. I can recommend you to use getters and setters in this way(for title field of class):

public String getTitle() {
    return title;
}

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

By this structure you can create a very pretty constructions while creating a new instance:

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

The full refactored version of your class:

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

}

Have a good coding!

Abzelhan
  • 510
  • 1
  • 7
  • 26