samedi 30 mai 2020

Should I build the object in the constructor?

I have a class called book consists off the book name and List of chapters. I read the information about the chapters ( such as the chapters name, chapter number ... etc) from some txt file on the hard desktop.

The book object itself is meaningless without loading the chapters. For best practices, should I write a build method and I build my object like the below ?

Book newBook = new Book(bookName).build();

Where build has the logic of loading the file form hard disk and filling the chapters list, or I should just make this build method private and create an object like the below.

Book newBook = new Book(bookName);

and inside the constructor of the book I should call the private method called build ?

Aucun commentaire:

Enregistrer un commentaire