So I wanted to reuse some functionalities like the try catch
and some other common functionalities used by my application. I was wondering if this approach is ok. Or if there is any other approach that would be great. I want to learn the different ways on how I can implement this.
This is a callback function that services will use.
public interface ClientOperation <T>{
T doAction(SomeObject someObject);
}
And this is the interface which will execute the callback function of ClientOperation
public interface Executor {
<T> T doTask(ClientOperation<T> operation);
}
The implementation class of Executor
public class ExecutorImpl implements Executor {
public <T> T doTask(ClientOperation<T> operation) {
T response = null;
/*
DO SOMETHING FIRST - USED BY ALL CLASSES
*/
try {
response = operation.doAction(someObject);
} catch (SomeException e) {
/*
DO SOME SERIOUS STUFF
*/
} catch (Exception e) {
LOGGER.error(e.getMessage());
throw new SystemException(e.getMessage(), e);
}
return response;
}
}
A service that does something and implements the callback function
public void addSomething(final Secret secret)
throws UnauthorizedActionException, InvalidSecretKeyException {
executorService.doTask(new ClientOperation<Response>() {
public Response doAction(SomeObject someObject) {
//DO STUFF ON THIS
return template.write(secret.getPath(),
secret.getSecretValue());
}
});
}
Aucun commentaire:
Enregistrer un commentaire