Design and Implementation of AndroidEventBus

Source: Internet
Author: User

Design and Implementation of AndroidEventBus
1. Features

AndroidEventBus is an event bus library of the Android platform. It simplifies interaction between components or objects such as Activity, Fragment, and Service, and greatly reduces coupling between them, this makes our code more concise and less coupled, improving the quality of our code.

AndroidEventBus absorbs the advantages of greenrobot EventBus and square otto, and makes corresponding improvements on this basis, making the event bus framework more suitable for users, it also makes event delivery more accurate and flexible.

Differences from EventBus and otto
Name Whether the subscription function can be executed in other threads Features
EventBus of greenrobot Yes Use the name pattern mode, which is efficient but inconvenient to use.
Otto of square No Annotations are easy to use, but they are less efficient than EventBus.
AndroidEventBus Yes Annotations are easy to use, but not as efficient as EventBus. The subscription function supports tag (similar to the Action of the broadcast receiver) to make event delivery more accurate and adapt to more scenarios.

For more details, see AndroidEventBus and AndroidEventBus open-source library release.

2. Overall Design

The simple overall design of AndroidEventBus is roughly shown in. It consists of three parts: EventBus, SubscriberMethodHunter, and EventHandler.

EventBus manages subscription objects and events, such as registration, cancellation, and event Publishing. When an object is initially registered to EventBus, EventBus traverses all the methods in the class of the object and manages the functions with the number of parameters being 1 and marked with the Subscriber annotation, construct an EventType Based on the event type and the Subscriber tag. An event type corresponds to a receiver list. When an event is published, EventBus constructs an EventType Based on the published event type and tag, finds the corresponding subscriber list, and delivers these events to all subscriber. SubscriberMethodHunter is responsible for finding the appropriate EventType, while EventHandler is responsible for executing these subscription functions into the corresponding thread. So far, the entire event bus operation process has been completed. Of course, when registering components such as Activity and Fragment to EventBus, do not forget to remove them from EventBus when these objects are destroyed, call the unregister method. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxoMiBpZD0 = "3-flowchart"> 3. flowchart: simple flowchart for registering a receiver

Simple event publishing Flowchart

4. detailed design 4.1 core categories EventBus: The Event bus core class encapsulates the registration, cancellation, event Publishing, delivery, and so on of the subscription object. EventType: an event type object, composed of the event Class type and the tag of the receiving function, is used to identify an event type. Subcriber: Subcriber annotation acts on a function and is used to identify this function as an event subscription function; threadMode: Execution thread model of the receiving function, which is executed in the UI thread by default; Subscribe: encapsulation class of a Subscription object, including the receiving object, target method, and thread model; TargetMethod: target Subscription method; subsciberMethodHunter: Subscribe Method Search helper class, based on the event object to find the list of eventtypes that meet the requirements, EventBus delivers the event to the subscriber Based on the returned list; EventHandler: Event processor; DefaultEventHandler, that is, the thread in which the event is shipped, and the processing event is also in this thread; UIThreadEventHandler: Handler of the event processing in the UI thread; AsyncEventHandler: Handler of the event processing in an independent thread; MatchPolicy: interface for event matching policy; DefaultMatchPolicy: the default matching policy. When an event is published, EventBus searches for the function whose parameter is the parent class of the event and constructs the corresponding EventType; StrictMatchPolicy: strict matching policy. Only subscription functions that fully match the parameter type are found when an event is published. 4.2 class relational graphs

5. Miscellaneous

The basic structure and design of the event bus are similar, but some functions have been selected or enhanced between various feature to meet the specific needs of users. The basic features of several event bus libraries are outlined in Chapter 1. You can select one based on your needs.

In this article, we will introduce AndroidEventBus in detail, so stay tuned.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.