Concept
The best way to solve the problem of too many event classes is to use Static Nested Classes . You can learn more about them here .
Now, using the above concept, you can solve the problem:
Basically, suppose you have a class called "Doctor" that you use to create an object that you are viewing with your application. However, you want to send the same object over the network and receive JSON in the context of the same object and pass it to the subscriber to do something. Maybe you will create 2 classes
- DoctorJsonObject.java , which contains information about the returned JSON data and
- DoctorObject.java , which has data that you are viewing in your application.
You do not need to do this. Instead, do the following:
public class Doctor{ static class JSONData{ String name; String ward; String id;
You now have one class of doctors that encapsulates both events for messages on the network and messages from the network.
To use an object of the AppData class, then for the post event:
public function postRequest(){ bus.post(new Doctor.AppData("doctors_username","doctros_password")); }
The subscriber in your implementation that listens for this object and makes an HTTP request and returns Doctor.JSONData:
@Subscribe public void doctorsLogin(Doctor.AppData doc){
In the above implementation, you encapsulated all the objects associated with the Doctor in the class ONE Doctor and gained access to various types of objects that you need at different times using static inner classes. Less classes have more structure.
source share