Suppose I have the following base class:
public class RootClass { private List<RootClass> children = new ArrayList<RootClass>(); public RootClass(RootClass parent) { if (parent != null) { parent.addChild(this); } } public void addChild(RootClass child) { children.add(child); } }
And I have the following child class:
public class ChildClass extends RootClass { public ChildClass(RootClass parent) { super(parent); doInitialization(); } private void doInitialization() {
Now suppose I create an instance of ChildClass that throws an exception, but falls outside the scope of the constructor (somewhere deep in the code, so far away, it does not know that ChildClass has not been completely built). Now the parent has a link in his children list to the ChildClass , which did not have its own constructor.
How to deal with these situations? Is this just a bad design scheme? Or does it make the least sense? I really don't want to reorganize the whole application to add a parent to a parent after the constructor completes.
source share