I have a hierarchy of worker
classes, all of which do some kind of processing to a workpiece. The idea is, that each worker does some pre processing, pushes the workpiece to the subclass and then does some postprocessing:
public void process(Workpiece wp) {
doPreprocessing(wp);
sub.process(wp); // this obviously doesn't work
doPostProcessing(wp);
}
Right now I'm solving this by declaring new abstract methods:
public final void process(Workpiece wp) {
doPreprocessing(wp);
subProcess(wp); // this obviously doesn't work
doPostProcessing(wp);
}
protected abstract void subProcess(Workpiece wp);
which has the obvious disadvantage that for each hierarchy-level, there is an additional, new method.
I would like to guarantee, that all pre- and post-process methods are executed even with new, user-implemented workers, which are not under my control.
How would you do that?