Advantages and disadvantages of using EventBus in java projects

       In a java project, if it is not swing, when the event and notification mechanism is used, the most fluent and easiest way is to use

Google's guava-EventBus, which is concise, powerful, and easy to use, is the first choice for most open source enthusiasts.

       The benefits of using EventBus are:

      1. EventBus is concise and powerful

      2. No need to write callback interface

      3. Can communicate with each other between components and threads

 

       However, it also has drawbacks. For example, the handling (listening) of an event is determined by the parameter type of the method . Tracking down and locating problems can be a pain when using EventBus heavily in a project. We know that the three elements of an event are: the event source, the event state, and the event handler. That is, when a state of an event source changes, the handler of the event listens to it and handles it accordingly. The registration event of EventBus is only registered through EventBus.register(). When an event has N listeners, EventBus.register() will be distributed in N corners of the project, and when there are N kinds of events, this situation becomes more scary. When locating the problem, it is impossible to determine how many listeners an event will have, because you cannot know how many register() there are in the project, and how many method parameters are of the same type as the post() parameters.

       In contrast, the event handling of javascript and actionscript is much more friendly. Such as: btn.addEventListener(MouseEvent.CLICK, onClickHandler); This line of code can express the three elements of the event. The event source is btn, the event state is click, and the event handler is the onClickHandler function. Very good positioning problem. Even if a global event such as Google's EventBus is implemented through javascript and actionscript, it can be quickly located through the event state, who is the event listener, and how many event listeners there are.

      Personally, Google's EventBus is more suitable for use in android projects, especially in the communication between activities. It seems that traditional projects are still not good, and they are still implemented through callback interfaces or observer patterns. Because it is more intuitive and better to track and locate problems.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326361506&siteId=291194637