Home java What is the EventBus library or its analogue otto?

What is the EventBus library or its analogue otto?

Author

Date

Category

looked through many links in Google , but still did not receive a specific answer: what kind of task is the library greenrobot eventbus (or its analogs – Square Otto ) What is better than standard options and how much does it justify your use?


The last question arises due to the fact that I can not imagine such an architecture built on events. In MVP \ RXJAVA, the library does not fit at all (at a minimum, in my projects I did not see places to use), except that it makes sense to use the library in communication between service and other parts of the application.


Answer 1, Authority 100%

If you distract from all sorts of wise terms, like event-oriented programming, this library is used to organize communications (data exchange and events) between non-related parts of the application.
That is, this library allows you to easily send arbitrary data from one part of the application (for example activat) to another (for example, a fragment).
The work is based on the registration of the event receiver and then send some events “to the ether”. The desired receiver will receive its signal and you can accept the data sent. Example:

First creates a class model with initialization designers, which will store the transmitted data:

Public Class MessageEvent {
  Public Final String Message;
  Public MessageEvent (String Message) {
    This.Message = Message;
  }
}

Here class-model MessageEvent with one field of data type String and the initialization constructor of this data. You can create an arbitrary number of data fields of arbitrary objects, and initialization designers may include several arguments.

Then in the class (for example activat) from which it is necessary to transmit our data created an event:

eventbus.getdefault (). POST (New MessageEvent ("Hello Everyone!"));

In class, where we wish to send our data (for example, fragment) We register the event receiver:

@OVerride
  Public void onstart () {
    super.onstart ();
    // Registration of the receiver at the start of the fragment
    EventBus.getDefault (). Register (this);
  }
  @Override
  Public void onstop () {
    // unsubscribe from registration when closing a fragment
    EventBus.getDefault (). Unregister (this);
    super.onstop ();
  }
  // In this method-colol, we get our data
  // (Object `Event` Type class-model MessageEvent)
  Public Void OneVENT (MessageEvent Event) {
    // Remove the sent string from the model: event.message = "Hello Everyone!"
    Toast.maketext (getActivity (), event.message, toast.length_short) .show ();
  }

As soon as the activist is executed the sending code in the column Onevent () Fragment will appear sent data. You will receive these data only those receivers that the signature with the sent event coincides in the columke.
In addition to the class classes, you can transfer simple data types, for example:

Sending:

int num = 5;
 EventBus.getDefault (). Post (NUM);

Receipt (receiver registration omitted):

Public Void Onevent (Int Event) {
      Toast.maketext (getActivity (), "Number:" + event, toast.length_short) .show ();
  }

In general, it does not matter that you are shlelet in the bus: classes-models of complex structure, objects, collections, arrays or primitive types – all this will be safe delivered to the recipient.

Data exchange tools that implement the Android system itself, this solution is distinguished by simplicity and transparency of use, the absence of any connection between the sender and the recipient and the ability to transmit data of any type and in any amounts without any parcelibization or serialization.

The library also allows you to send “sticky” events (will not disappear until other data go, for example, when transmitting between two activists, when the receiver event has not yet existed, the data will be received when the recipient appears), Work with asynchronous streams and so on. See the documentation for details.

also have other libraries of similar functionality, for example square otto .
In addition, stepped far further the concept of reactive programming – framework RXJAVA and add-ons for Android – framework rxandroid , taking into account the specifics of the platform.


Answer 2, Authority 43%

for code decomposition.
Suppose you have activat + a few fragments, without the “tire” you are from activat, they block the Kalbeks, or in a fragment of the fragment of Castitis to the activist class (or interface that implements this activat) and call methods. From the bus you are in activati ​​sign up for events and deposit event fragments. The code about the colbet goes, less code is clearer. Interfaces are “unleashed” fragments easier to re-use in other activati.
Or another example with business logic, we write as a separate module that knows nothing about UI and communicates with it through Event Bus. Less dependencies – it is clearer code, when testing we replacing the UI test code, which will post / subscribe to events related to business logic.
Those. All this will not make your code faster, the task you do not consider better.
All this is rather a question “faith”, in the sense of development methodology.
By more account, all techniques in programming (functions, OOP, etc.) is a task decomposition. You have a big task, you beat it into small subtasks. The submodule is isolated from others, the clearer.
I use a similar library http://square.github.io/otto/ + dependency injection http://square.github.io/dagger/ It turns out nice.
But again, I repeat all this personal selection of a programmer / commander. Allows you to write more clear? – We use. Those. Not that “here is Event Bus and we all take it and use everywhere.”

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions