us if such a business scenario. Now in a timely chat app, we send and receive messages on the chat page. At the same time also want to update the previous page of the chat record, then we how to achieve? Tell me about the strategy I've implemented. I'm using a broadcast receiver, Broadcastreceiver, to send and receive messages when they are received and sent, and then receive updates where they need to be updated in real time. The realization of the idea is relatively simple, there is no code on the coupling problem, but there is a disadvantage. The drawback is the need to implement broadcastrecevier in very many places, although the code is not redundant, but much more, it looks very uncomfortable.
Today's introduction to Otto. Can completely solve the problem of code volume. Otto is a popular event bus framework that is designed to maintain efficient communication between the application pages and modules and decouple them accordingly. The use of Otto is very easy, the source code is only 9 classes, thousands of lines of code, the open API is also indirectly clear. Otto is a design pattern based on subscription/announcement (Subscribe/publish), simply put. Assuming you want to subscribe to a message, you can receive it using @subcribe annotations, using
Bus.post (Object obj) publishes the message, and the design achieves complete decoupling.
The following steps are for the introduction of the use of the process.
First, bus instantiation
Bus This class is the soul of the whole frame, it is responsible for the announcement and reception of the message, the whole process is through this bus to achieve. Bus instantiation is recommended to use a single case, that is, the entire application only instantiate a bus object, all the processing of the message is through this single instance to achieve.
The advertised message is received by the recipient who is implementing the message. Be sure to go through the same bus object processing. The bus's constructor is capable of receiving threadenforcer-type parameters, and Threadenforcer is actually an interface, which itself has two implementations, each of which indicates whether the bus executes in the main thread or an asynchronous thread.
II. Registration and binding bus
According to the detailed business requirements for the bus's registration and binding, for Android components, usually based on the life-cycle approach to achieve. At the same time, assume that you can do it in a class that you define yourself. The following shows are implemented in activity and fragment.
Iii. publication of the News
Announcing the message is the most important part of the entire framework, and it agrees that you tell all Subscribers that an event has been triggered.
Any instance object of a class can be advertised through bus buses. At the same time it can only be received by recipients who subscribe to such objects. The following shows a message via bus that the content of the message is locationchangeevent, so the recipient of Locationchangeevent can receive this announcement.
Note that the announcement message can only be a single Object object.
Iv. Subscription of messages
The subscription and publication of the message is preceded by a bus registration in the current class. A subscription is a supplement to the announcement of a message, and the corresponding message subscriber can receive the message immediately after the event is called.
The implementation of subscription functionality is achieved by means of its own definition. The name of the method can be arbitrary. At the same time, there are three conditions to be met.
1, the method before using @subscribe annotations
2. Access modifier is public
3. A single number of references. Set according to the message you want to subscribe to
Note: Prior to use, remember to register and use to complete. Remember to release.
V. the produce of the news
When the Subscriber registration is complete, specific messages are targeted. It is also often necessary to obtain the currently known value. This time, you need to use the produce. the same method name using produce can be arbitrary, at the same time there are three points to be aware of.
1. Use @produce annotation before method
2. Access modifier is public
3, no participation. The return value is based on the Subscriber parameter type
All right. That's the way Otto uses it.
Go ahead and refactor the code to get rid of the endless radio!
Of course, Otto's shortcomings are also some. To achieve the functionality of the subscription/publication model above, the cost is to reflect the classes of the individual bus. Assuming a large number of use cases, the corresponding performance is somewhat influenced by the number of pairs.
Otto Source code and demo address: Https://github.com/square/otto
Suppose that it is helpful to you. You are welcome to subscribe to my public account-Android dry sharing (id:android_share). The following is the QR code, to provide you with timely high-quality Android dry.
Technical Exchange QQ Group: 318588906, welcome everyone to add a group, together to explore the next Android and Java technology. Grow our dry-sharing community together.
Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.
Event Bus Frame---Otto