Benefits of Otto Event Bus Injection Instead of Using Static Singleton

In my Android apps, I use Otto as an event bus and Dagger for dependency injection.

In userguide Otto and many blog posts, it is recommended that you use injection to get a singleton on the bus. I have done this for some time, but lately I have become more doubtful if bus injection has any advantages over using a simple static singlet.

When injecting, I have to inject each user View or ViewHolder so that I can post user interface events on the bus. Especially with a dagger, it seems a bit awkward to introduce every class where I need a bus. Of course, I could pass the bus using the constructor or setter method, but it can be pretty awkward if you think of an adapter with many different types of views, for example.

And I do not see any benefits when injecting the tire. In the case of Otto, a specific implementation (bus instance) is introduced, and this will never change. Otto's wrapper for de-communication does not make any sense if you think because of how the subscription works.

So, does anyone see any benefits of an Otto injection that I don't see?

+6
source share
1 answer

In my opinion, you should definitely wrap the event bus in your class and use dependency injection methods to pass them to clients.

enter image description here

There are several advantages to this approach in simply getting the link through a call to the static getInstance() method:

  • Your dependencies become apparent. When you get references to objects through static calls, dependencies are hidden inside the client implementation, which makes the code fragile and harder to understand.
  • It will be easier to switch to another implementation of the event bus if such a need arises.
  • Nested dependencies are easier to dare in tests
  • The fact that addiction injection methods introduce some degree of struggle is actually a good thing - if you fight, it often indicates that you are doing something wrong. In your case, I suspect that you are abusing the event bus.

I say that it’s possible that you are abusing the event bus because I really don’t understand why you need to have a link to it in subclasses of View . I assume that you send notifications of user interactions to the event bus, and then subscribe to the Activity or Fragment on the event bus to intercept these events. Event dispatcher is the wrong tool in this case (although it works great).

The reason the event bus is the wrong tool in this case is because Fragments and Activity can have direct access to the contained View objects. You can get links to these Views and register Fragments and Activities as listeners. No need to separate anything here.

On the contrary: think about the case when you go and reorganize your Views in such a way that nothing more is sent to the event bus (for example, business requirements have changed). Since Views notifications are only related to what contain Fragment or Activity via the event bus, it is very likely that you will forget to remove the event processing logic from Fragment and Activity , leaving the “dead” code behind. This is very unpleasant.

It would be best practice to use the Observer and Views design pattern to notify Activities and Fragments directly, and only when processing involves another component (which cannot be easily reached from Fragment and Activity , such as another Fragment or Activity ), will these components send events to event bus. If you follow this approach, you will need to have a link to the event bus only in the top-level components, and no fight will be involved.

PS I recently posted a blog post that provides some guidelines for implementing dependencies in Android .

+1
source

All Articles