我解决了太多事件类问题的最好方法是使用静态嵌套类您可以阅读有关它们的更多信息here.
现在使用上述概念就是如何解决问题:
因此,基本上假设您有一个名为Doctor的类,您正在使用它创建一个与您的应用程序一起传递的对象.但是,您希望通过网络发送相同的对象,并在同一对象的上下文中检索JsON,并将其反馈给订阅者以执行某些 *** 作.你可能会创建2个类
> DoctorjsonObject.java,包含有关返回的JsON数据的信息
> DoctorObject.java,包含您在应用中传递的数据.
你不需要这样做.
而是这样做:
public class Doctor{ static class JsONData{ String name; String ward; String ID; //Add your getters and setter } static class AppData{ public AppData(String username,String password){ //do something within your constructor } String username; String password; //Add your getters and setters }}
现在,您有一个Doctors Class,它将发布到网络的事件和从网络发回的事件封装起来.
> Doctor.JsONData表示以Json格式从网络返回的数据.
> Doctor.AppData表示在应用程序中传递的“模型”数据.
要使用类’AppData对象,然后使用post事件:
/* You would post data from a fragment to fetch data from your server. The data being posted within your app lets say is packaged as a doctor object with a doctors username and password.*/public function postRequest(){ bus.post(new Doctor.AppData("doctors_username","doctros_password"));}
您实现中的订阅者侦听此对象并发出http请求并返回Doctor.JsONData:
/*retrofit implementation containingthe Listener for that doctor's post */ @Subscribe public voID doctorsLogin(Doctor.AppData doc){//put the Doctor.JsONObject in the callback API.getDoctor(doc.getDoctorsname(),doc.getpassWord(),new Callback<Doctor.JsONObject>() { @OverrIDe public voID success(Doctor.JsONObject doc,Response response) { bus.post(doc); } @OverrIDe public voID failure(RetrofitError e) { //handle error } }); }}
通过上面的实现,您已经在ONE Doctor类中封装了所有与Doctor Object相关的事件,并使用静态内部类在不同时间访问了您需要的不同类型的对象.更少的类更多的结构.
总结以上是内存溢出为你收集整理的android – 使用EventBus或Otto时减少事件类的数量全部内容,希望文章能够帮你解决android – 使用EventBus或Otto时减少事件类的数量所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)