Java: post-processing after subclass constructor completes
I want to define some common post-construction behavior for a group of classes. When you have shared behaviors in different classes, Java tells us to extract them out into a parent class.
Conceptually, it makes sense, we're saying, for objects of this type (and its subclasses), do some post-processing after it's constructed;
Practically, it's hard to do. You obviously can't put it in the parent class constructor beca开发者_如何学JAVAuse parent constructor is called before the subclass constructor. I could write a postInit() method in the parent class and require all subclasses to call it as their last statement in their constructors, but it doesn't seem very clean, as there's no way to enforce it, and people do forget.
Is there maybe some language construct that I'm not aware of that may solve my problem?
Thanks
Just a little more background on the requirement. Factory methods etc. that are suggested by many of the answers (which I've upvoted) below are all good ideas, but I don't really have that luxury. The actual situation is this, I have this parent class, which is extended by a couple of dozen subclasses, which are in turn used in many other places. So redesign how these subclasses are used is out of the question, and changing all subclasses is borderline possible. Ideally, I just need to change the parent class, so that's what I'm asking.
If you can't use a factory method for pragmatic reasons, the best solution I can think of is to create a protected final
method in the root class that does the post processing, and add a call to the method to each and every leaf class; e.g.
public abstract class Root {
...
protected final void finishInit() {
// Do post-processing
}
}
public abstract class Intermediate {
...
protected Intermediate(...) {
super(...);
...
// don't call finishInit();
}
...
}
public class Leaf {
...
public Leaf(...) {
super(...);
...
finishInit();
}
...
}
If Intermediate
is not abstract
then you need a separate public constructor to create instances that calls finishInit()
at the end.
It's all a bit clumsy, but that's the penalty you have to pay if you can't/won't refactor your code to use factory methods.
One solution is to not have any public ctor. You can have a factory method that finishes the initialization before returning the object to the caller.
Well - the most simple solution is to add a template for this hierarchy of classes in the IDE.
Did you think about your design and how it can be changed? May be you need some kind of design pattern, like Factory Method, Abstract Factory, Builder ?
精彩评论