This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
I am seeking advice on what should happen if constructor passed invalid data and unable to create new object. Is this responsibility of object class or calling class to manage this? This best illustrated with simple example below. Say have simple class called DayOfWeek. It only has once instance variable that stores day of week as number (1=Sunday,..7=Saturday). It simply stores day of week and has various format methods to return day of week eg Tues, Tueday etc. There is constructor that accepts integer parameter of day of week (1-7). If object instansiated with invalid constructor parameter (eg 8). Confused about what should happen to prevent invalid object being created. Do not wish to create DayOfWeek object that contains invalid data. Should constructor check if parameter is in valid range? If it is not, what should happen? Should it display message or how do you communicate failure back to creating module? Or Should there be a static isValid method in DayOfWeek class that calling module calls first to check if day is valid before attempting to instansiate (get NEW) DayOfWeek object? ie whose responsibility should this be? If pass in invalid parameter, just can't keep rolling on as if all okay. I have searched for this, but had no success. If I have missed please advise. Thanks in advance.
Should constructor check if parameter is in valid range? If it is not, what should happen? Should it display message or how do you communicate failure back to creating module? Sounds to me like you overlooked the exception throwing and handling mechanism in Java. Take a look at Sun's Handling Errors with Exceptions tutorial.
There are a couple of options. One is to throw an Exception which would cause the instantiation to fail. The other option is to use a class to insure that only valid data can be passed to your constructor:
Change your constructor to only accept DayOfWeekUnit objects. This would be a more advanced solution and really shouldn't be in the beginner's forum. [ October 06, 2003: Message edited by: Thomas Paul ]
Craig: I want to re-emphasize what Eugene and Thomas are saying about the use of exceptions in the type of situation that you describe. In general, if an object cannot be created, its constructor should throw an exception that indicates the problem. It is then the exception handler's responsibility to take appropriate action, such as fixing or reporting the problem. The situation of a failed instantiation is one of the classic reasons that exceptions are part of Java. Their use is fundamental to successful Java programming. To your question about whether a constructor should check that the data it is passed is valid. The answer is definitely Yes! No constructor should be allowed to construct invalid objects. As to using a static "isValid" method to check before constructing the object, this might be a useful convenience, but it won't prevent the problem you describe. The reason is that it does not actually prevent someone from attempting to construct an invalid object anyway. That is, I can call isValid, but ignore its result, or I can just attempt to construct an object without validating the data first. Again, the constructor must determine if the data that it is passed is valid. If it isn't, the constructor should throw an exception. Finally, Thomas' second option shows how you can head-off invalid data before an object is constructed. But as Thomas states, this is an advanced solution. You should explore it only after you fully understand the use of exceptions.
For my latest books on Java, including my Java Programming Cookbook, see HerbSchildt.com
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com