Created
August 26, 2015 14:22
-
-
Save go-cristian/8ea38bbbebaf02ec1e13 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Defaul base interactor. This class describes in the practical Clean Architecture how a use case | |
* it can be perfomed. For practical pruposes a use case has two phases the moment when is executed | |
* an the moment when needs to return a response, it depends on a default {@link InteractorExecutor} | |
* wich handles the basic flow. At the end of the process the {@link InteractorResponse} sends the | |
* response to the class that generates the call. For practical purposes an inner class model is | |
* needed and must be implemented for every {@link Interactor}. | |
*/ | |
public abstract class Interactor<E, J> { | |
protected final InteractorExecutor interactorExecutor; | |
protected InteractorResponse<J> interactorResponse; | |
private J response; | |
public Interactor(InteractorExecutor interactorExecutor) { | |
this.interactorExecutor = interactorExecutor; | |
} | |
/** | |
* Based on the command pattern, this method allows to run the unique function of a use case. | |
* | |
* @param innerModel | |
* @param interactorResponse | |
*/ | |
public void execute(final E innerModel, final InteractorResponse<J> interactorResponse) { | |
this.interactorResponse = interactorResponse; | |
interactorExecutor.execute(new Runnable() { | |
@Override | |
public void run() { | |
getResponse(innerModel, interactorResponse); | |
} | |
}); | |
} | |
/** | |
* Inner method that allows the execution ana manegement of the post an execution processes. | |
* | |
* @param innerModel | |
* @param interactorResponse | |
*/ | |
private void getResponse(final E innerModel, final InteractorResponse<J> interactorResponse) { | |
try { | |
response = onExecute(innerModel); | |
interactorExecutor.postExecute(new Runnable() { | |
@Override | |
public void run() { | |
interactorResponse.onFinish(response); | |
} | |
}); | |
} catch (final InteractorException e) { | |
interactorExecutor.postExecute(new Runnable() { | |
@Override | |
public void run() { | |
interactorResponse.onFailure(e); | |
} | |
}); | |
} | |
} | |
/** | |
* Abstract method used for every use case to perform is unique function, this allows to | |
* generate a response model used for an standar output. | |
* | |
* @param innerModel | |
* @return | |
* @throws InteractorException | |
*/ | |
public abstract J onExecute(E innerModel) throws InteractorException; | |
/** | |
* Gets the last response generated by the use case. | |
* | |
* @return | |
*/ | |
public J getResponse() { | |
return response; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment