Java template method pattern

334 Views Asked by At

I am trying to implement something along the lines of the template method pattern within some JavaEE beans that do some processing work on a dataset.

Each processing bean takes the Job object, does some work then returns the updated job for the next bean (about 10 in total), each bean has a single method with the same name (doProcessing) and single argument (job)

I would like to perform some logging at the start and end of each beans 'doProcessing' method so that at the end of processing the Job contains logging info from each bean (stored in a hashmap or suchlike)

My current implementation looks something like this...

@Stateless
public class processingTaskOne(){

    public void doProcessing(Job job){

        //always called at beginning of method
        String beanReport = "Info from Task 1: ";

        for(int i=0; i<job.getDataArray().size();i++){
            beanReport+="\n some more info";
            //Do some processing work here
        }

        //always called at end of method
        job.addNewReportSection(beanReport)

    }

}

But I know that I can do better than this, using inheritance I should be able to create a superclass along the lines of...

public abstract class Reportable{

    private String sectionReport;

    public void preProcessing(Job job){
        //Setup bean report, use reflection to get subclass name 
    }

    public void postProcessing(Job job){
        //Finish bean report and append to job
        job.addNewReportSection(sectionReport)
    }

    public abstract doProcessing(){
        //not sure how this should work
    }

}

And any class that extends the superclass will automatically perform the pre/postprocessing actions...

@Stateless
public class processingTaskOne() extends Reportable{

    public void doProcessing(Job job){

        for(int i=0; i<job.getDataArray().size();i++){

            super.sectionReport += "log some info"
            //Do some processing work here

        }

    }

}

But I have been unable to work out how to implement this exactly, since all the examples refer to POJO's and as my beans are @Stateless there is no constructor.

Can someone provide some guidance on this? Am I barking up the wrong tree?

1

There are 1 best solutions below

1
On BEST ANSWER

If I understood your question correctly, you can try the following:

public abstract class Reporting {
  public void setUp(Job job) {
    // set things up
  }

  public void tearDown(Job job) {
    // post-processing stuff
  }

  public void process(Job job) {
    setUp(job);
    doProcessing(job);
    tearDown(job);
  }

  public abstract void doProcessing(Job job);
}

public class Processor1 extends Reporting {
  @Override
  public void doProcessing(Job job) {
    // business logic
  }
}

and later, somewhere in your code, you should call not the doProcessing(), but rather the process() method of your base class.

Also, in case my interpretation was correct, you might be interested in using some aspect-oriented programming framework like AcpectJ or Spring AOP.