I have these Worker classes doing some job:
class WorkerOne implements IWorker {
@Override
public doWork(Resource resource){
// do some work
}
}
I want all doWork() methods to be wrapped by the same operation.
One way to achieve it is creating an abstract class as the following example:
abstract class WorkWrapper implements IWorker {
@Override
public doBetterWork(){
Resource resource = // ...
doWork(resource)
resource .close();
}
}
class WorkerOne extends WorkWrapper {
protected doWork(Resource resource){
// do some work
}
}
and invoke the Worker as:
WorkerOne worker = new WorkerOne();
worker.doBetterWork();
For some reasons I prefer not to use inheritance. Is there a better solution to do this wrapping?