|
Don't forget about this condition:
At that point your object has "settled" on its identity. Only after this point, it's ok to put this object into a hash structure.
This is difficult to enforce with the flag approach. The only alternative that I know of is to not allow the object to exist and not be in its settled state, which would mean getting it to a settled state by the time the constructor is finished.
Exception: if object construction happens entirely behind a factory or DAO. This can be difficult to accomplish. For example, what scope do you give the constructor so that the factory/DAO can access it but client code can't (including subclasses of the factory/DAO if not final). Then, you're left with the task of carefully reviewing the factory/DAO code to make sure it doesn't violate the rules. It certainly can be done, and I'm sure there are cases where it's necessary, but I would tend to avoid it if possible. |