java - Value checking logic inside or outside of a class? -
Take this skeleton class that any web site wants to fill the RSS stream on a series of sites:
Now, we think that the fresh time is more than zero and that the RSSURL is a valid http address Should be.
The obvious reflex has to put some value test logic inside the constructor, however, the constructor gives the caller a call to an object immediately, whatever happens. This means that if the objects do not allow it to work, the object becomes useless. It also means that the object must be drum or reused.
So, here are some questions on the subject:
- Some classes are a getInstance () probably a private producer? If I remember well, then an example would be the Gregorian calendar.
- In what case would you use such an approach?
- In most cases do you have logic in your constructor?
- If so, do you apply to the unit-style classes used in the context of the persistence of the domain model or not?
All your answers are welcome, it would be interesting to get a clear view of the most common practice.
Some of the most common things:
-
If you use for building goods, you will usually be a private producer and will need urgently through the factory. It supports the switching of construction strategies with the factory.
-
A custom (and hopefully, informative) will be thrown for me in the manufacturer if the exception is not immediate:
public class person {
Public person (name of the integer ID, string) throws invalid process option if throw (invalid name ==) new invalid PersonException ("You are a person without any name"); ...
-
This unit is likely to cause disturbance in persistency objects, mostly because you want these objects to be logic-free, and because the structure Take care of it for you - eg if you have bean containing (id, name) in hibernate and stay with the name to be redundant in a table, the error throws DB or your configuration should be sufficient.
Comments
Post a Comment