EventDispatcher event distribution component and android event distribution mechanism

Source: Internet
Author: User
Tags autoload

EventDispatcher event distribution component and android event distribution mechanism

Introduction

Now you want to provide a plug-in system for your project. You can add some methods to the plug-in, or do something before or after some methods are executed without interfering with other plug-ins. To implement this system, a simple single inheritance method is not a good solution. Even if it is possible to inherit more in PHP, it also has inherent disadvantages (not familiar with many inheritance, ).

Symfony EventDispatcher implements the intermediary mode in a simple and effective way. The event distributor is the intermediary, so that the system and the plug-in are not coupled, which makes the above plug-in system possible, and it will make your project more scalable.

Translated from official Symfony documentation

System Analysis

Event Storage

The figure above shows how events are stored in the system by analyzing the source code of the Symfony EventDispatcher component.

The event is stored twice to add the priority of priority, put the event in the structure above, and take it out of the structure below when it is retrieved, the same event name can have different priorities. events with a higher priority are triggered first. events with the same priority are inserted first.

Sorting events (in the lower structure) are not constructed when an event is inserted. Instead, sorting events are generated when events are retrieved, when a new event is inserted in the same event name or an event is deleted, the corresponding sorted event name is deleted and re-constructed when used later.

When an event is executed, the linster list sorted by the corresponding event name is obtained and executed in sequence.

Event execution

As shown in, when multiple trigger actions are monitored under the event name at a time, they are triggered in order of priority and registration, the trigger action is generally an executable "instance" (whether it is a class or a function, it must be called through call_user_func_array). Three parameters can be passed in. The first parameter (required) is an Event instance, the second is the Event name triggered, and the third is the Event distributor instance. The first parameter controls whether the Event is passed between all the trigger actions under the Event name. For example, in linstener_2. propagationStopped is set to true. After linstener_2 is executed, the event will stop spreading and the action after linstener_2 will not be triggered.

In addition, the Event instance can save other necessary information so that the linstener can obtain additional information when it triggers execution.

Event subscriber

The Event subscriber tells the dispathcer instance that all the events to be subscribed to do not need to be registered by dispathcer instances one by one. The event subscriber is a PHP class that tells dispathcer the specific event to subscribe.

Benefits:

  • The following events do not need to be registered one by one.
  • You do not need to remove the registration to cancel the follow events one by one.

The subscriber's internal attention events are a whole, either all or all

Instance

Common chestnuts

include "vendor/autoload.php";use Symfony\Component\EventDispatcher\EventDispatcher;use Symfony\Component\EventDispatcher\Event;class UserEvent extends Event{ public function name() { return "Cartman"; } public function age() { return "24"; }}$dispatcher = new EventDispatcher();$dispatcher->addListener("user.name", function($event, $eventName, $dispatcher){ echo "My name is Cartman\n";});$dispatcher->addListener("user.name", function($event, $eventName, $dispatcher){ echo "My name is {$event->name()} from Event instance\n";}, 10);$dispatcher->addListener("user.age", function($event, $eventName, $dispatcher){ echo "My age is 24\n";}, 10);$dispatcher->addListener("user.age", function($event, $eventName, $dispatcher){ echo "My age is {$event->age()} from Event instance\n";}, -10);$dispatcher->dispatch("user.name", new UserEvent());$dispatcher->dispatch("user.age", new UserEvent());

Output in the above example

My name is Cartman from Event instanceMy name is CartmanMy age is 24My age is 24 from Event instance

Event subscriber

Register an event through Subscriber

Include "vendor/autoload. php "; use Symfony \ Component \ EventDispatcher \ Event; use Symfony \ Component \ EventDispatcher \ EventSubscriberInterface; class BookEvent extends Event {public $ name = self: class;} class BookSubscriber implements EventSubscriberInterface {public static function getSubscribedEvents () {return ["chinese. name "=>" chineseNameShow "," english. name "=> [[" englishNameShow ",-10], [" englishNameAFter ", 10],]," math. name "=> [" mathNameShow ", 100];} public function chineseNameShow (Event $ event) {echo" I am a Chinese book \ n ";} public function englishNameShow (Event $ event) {echo "I am an English book \ n";} public function englishNameAFter (Event $ event) {echo "I am an English book displayed later [from Event instance {$ event-> name}] \ n";} public function mathNameShow (Event $ event) {echo "My mathematical books \ n" ;}$ dispatcher = new EventDispatcher (); $ subscriber = new BookSubscriber (); $ dispatcher-> addSubscriber ($ subscriber); $ dispatcher-> dispatch ("english. name ", new BookEvent (); $ dispatcher-> dispatch (" chinese. name "); $ dispatcher-> removeSubscriber ($ subscriber); $ dispatcher-> dispatch (" math. name ");

Output content:

I am an English book after presentation [from the Event instance BookEvent] I am an English book I am a Chinese book

The above is all the content of this article. I hope this article will help you in your study or work. I also hope to provide more support to the customer's home!

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.