This is a tutorial for introducing a how implement, usage, and test callback structure, for example: when you need to return different structures on one method:
// Usage a kind of callback method
service.needImagesForSomethig(id idTrack,String hashSecurity,newServiceCallback(){publicvoid successful(int code,Array<Bitmap> maps){// ok response}publicvoid throwProblem(int code,ServiceException exception){// When you have a problem. f.e: error connection or exception}});
Dependencies
Knowledge intermediate programming
2. Getting set up
To start you need a code with interfaces classes or adapter patterns.
Download the Code
Click the following link to download all the code for this tutorial:
Second add a method that return the own controller/api (builder pattern)
public myController enviarData(int num,float[] decimals){
instance.mModel =newModel();
instance.mModel.setNumero(num).setCifras(decimals);return instance;}
Third add getRest method, to provide add method after sendData finished:
publicvoid enqueRest(RestCallBack restCallBack){if(mModel !=null)
restCallBack.httpOK(mModel.getNumero(), mModel.getCifras(), mModel);else
restCallBack.errorHttp(newNullPointerException("modelo sin construir"));}
Now, when you want to use it, just call:
myController.enviarData(0,newfloat[]{}).enqueRest(newRestCallBack(){@Overridepublicvoid httpOK(Object...objects){int num =(int) objects[0];float[] floats =(float[]) objects[1];Model model =(Model) objects[2];}@Overridepublicvoid errorHttp(Exception exception,Object...objects){if(exception ==null)
show(exception.getMessage());}@Overridepublicvoid successed(Object...objects){
show(objects[0].toString());}@Overridepublicvoid failed(Object...objects){
show(objects[0].toString());}});
5. Write callbacks (3)
Write an abstract class from three interfaces:
At the beginning, write 3 different interfaces :
// First interfacepublicinterfaceSuccessCallback{void onSuccess(Object... objects);}// Second interfacepublicinterfaceFailCallback{void onFail(Object... objects);}// Third interfacepublicinterfaceParseCallback{void onParse(ParseException exception,Object... objects);}
Subsequently, write an abstract class which implement the 3 interfaces that we wrote them.
At the beginning, in this part use the Callback interface from Write callbacks(1) , second :
// Attach too many callbackspublicvoid twoSteps(Callback...callbacks){for(Callback callback: callbacks){if(callback instanceofPreCallback)
callback.successed("Pre-Success");elseif(callback instanceofPosCallback)
callback.failed("Pos-Fail");}}
Lastly, write a statement that call the twoSteps method:
myApi.twoSteps(newPreCallback(){@Overridepublicvoid successed(Object...objects){
show(objects[0].toString());}@Overridepublicvoid failed(Object...objects){
show(objects[0].toString());}},newPosCallback(){@Overridepublicvoid successed(Object...objects){
show(objects[0].toString());}@Overridepublicvoid failed(Object...objects){
show(objects[0].toString());}},...// You put all callbacks, that you want them);
7. Write callbacks (5)
Write an api/controller class to use with different models:
At the beginning, write the class with templates annotations to support two different models:
Writing the interfaces:
// first interface with two template classpublicinterfaceIntTemSuper< A, B >{
A methodSuperReturnA(B b);}// inheritance interface from IntTemSuperpublicinterfaceIntTemUnder< X, Y >extendsIntTemSuper< X, Y >{
Y methodPoorReturnY(X x);}
Subsequently, write a main/api class, as a method, just do one thing and that it.
// Class with two template classespublicclassTemplateController< G, H >{publicvoid methodStaticCall(TemplateCallback< G, H > callback){
callback.methodSuperReturnA(null);
callback.methodPoorReturnY(null);}publicstaticabstractclassTemplateCallback< TC, UC >implementsIntTemUnder< TC, UC >{}}
Now, when you want to use it, build a new Class and call it.
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and limitations under the License.