I'm wondering what the best approach is to verify that a class has all the required details before it can be used.
For example say I have a Book
class and I want to pass in a set of properties about the book to the Book
constructor.
Book book = new Book(bookProperties);
What I want to make sure that BookProperties
is complete, i.e. has all the information.
Let's say in this example I have the following:
- Book Title
- Book Author
- Book Original Publishing Date
One way is that I could create a default constructor that only accepts all 3 items:
BookProperties bookProperties = new BookProperties("2001: A Space Odyssey",
"Arthur C. Clarke",
1968);
Now this is ok, if we only have three values, but say my class has 10 or more properties that need to be initialized by the user before the Book
class can be created.
One thing I was thinking was having a method in the BookProperties called isValid
. Then in the constructor of the Book
class I would see if bookProperties.isValid
and assert if the return is false.
Is this a good idea or am I going about this all wrong?