EventBus Android Tutorial – Using the Observer Design Pattern

posted in: Android, Design Patterns, Tutorial | 0

EventBus-Publish-SubscribeFor this tutorial we’ll be discussing the observer design pattern using Green Robot’s EventBus library.

The observer pattern allows us accomplish something extraordinary in software architecture. What that allows us to accomplish is low coupling, high cohesion. What that means is, we are able to avoid connecting all of our class instances together via references in code, but maintain a system of these objects that communicate via an event bus system. In other words, instead of passing references of objects back and forth to allow us to make a callback on a parent activity, for example, we can use EventBus instead. You’ll begin to understand this better when we go through the code in Android Studio.

So low coupling refers to limiting the number of connections between classes. High cohesion refers to the degree to which functionality belongs to a class. In other words, does a car object have a “change tire” function or does that belong to the mechanic object? Having objects contain logic that belongs to that class, makes the over all application have a higher robustness, reliability, reusability, and understandability. I use this principle in every application that I write.

EventBus helps us obtain this exact thing by having a class instance register with EventBus to start receiving events in the app. Another object, say a child activity, then creates an event and sends that out to EventBus. If it’s the same event that the first object cares about, it’ll get notified. Never does that second object need to know about the first, or its interface. That’s what we call low coupling! These events that EventBus uses, are just plain old Java objects that you can create and pass around. If you want an event fired off when the user logs in, let’s say, you can create a new instance of your UserLoggedInEvent and send that to EventBus. EventBus will notify any objects in our application that have registered a callback for that event.

Event Bus works by registering as a subscriber to the event. This will be our Main activity as it will subscribe to the event posted by our child activity. Namely the data that we’ll generate in that child activity. We’ll create an event and pass that to Event Bus from our child activity. Our child activity is known as a publisher.

We’re going to create a quick application that has two different activities. We’ll have our main activity and it’ll have a textfield to display some text that’s captured from our child activity. Normally, you might pass a reference to the main activity to the child activity so that the child activity can call a function that exists in the main activity. If you’re really smart, you might even have an interface that the second activity has to adhere to in the main activity so you can keep the interfaces between them civil.

This is a bad approach since it’s not scaleable and can quickly get out of hand. Imagine the case where we have multiple objects that care about the data that’s generated in that child activity. This starts to look like our network closet in a previous slide.
With EventBus, we would instead register both objects to subscribe to an event when the data is created in the child activity.
How easy is it to setup event bus? It’s as easy as 5 simple steps. Watch the tutorial video on setting up EventBus.
youtube-subscribe

 

 

Source Code and Example Code

Download: Electronic-Armory-EventBus

Download: Electronic Electronic Armory EventBus Slide Presentation

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.