An event listener is first and foremost just another type of service. This gives you the advantage of being able to isolate your event listener and use dependency injection. This gives you an ease of unit testing that the normal hook system cannot provide. You can create an event listener manually or use the Drupal Console to do by it by running drupal generate:event:subscriber and following the prompts:

The Drupal console updated the mastering_drupal_8.services.yml file to add the new service with the following:
mastering_drupal_8.event_subscriber:
class:
Drupal\mastering_drupal_8\EventSubscriber\MasteringDrupal8EventSubscriber
arguments: []
tags:
- { name: event_subscriber }
The event_subscriber tag is what marks this service as an event listener. In your event listener, you need to implement a static getSubscribedEvents() function that returns an array of event names with corresponding functions. Each event handler receives a Symfony\Component\EventDispatcher\Event object or subclass, which contains information about the event. This event object event allows you to cancel the event in the same way that you can for JavaScript events.
