tags:

views:

112

answers:

3

I'm using callables quite often , and I've stubled upon a question that irritates me:

Lets say that to run function foo() , one needs to do a couple of checks first.

Should you 1. Insert the checks as part of the Callable :

class A implements Callable<Long> {
...
public Long call() {
    check1();
    check2();
    return (run()); 
}
  1. OR , insert all this logic into another class (ALogic) and use the Callable a mere shell for the executor? class A implements Callable {
    ...
    public Long call() {
    ALogic aLogic = new ALogic();
    return (aLogic.run());
    }

What do you think are the pro's and con's? What do you usually prefer?

A: 

Which do you feel is simpler or clearer? I suggest you do that.

Peter Lawrey
+1  A: 

My general advice when implementing callback [Java keyword] interfaces is concentrate on making the [non-Java keyword] interface appropriate for the called type. There generally shouldn't be that much in the anonymous inner class (or whatever), but more than just forwarding call.

Also, it's generally not good to have an object that is constructed and then only has a single method called on it. Make it a static method (which may, perhaps, in turn create an object through a private constructor and run that).

Tom Hawtin - tackline
A: 

I usually prefer to simply forward the callback to a private method on the enclosing class. This eliminates the "this" reference that simply points to the anonymous inner class, which is pretty useless.

Phil