Introduction to Event Handling

Anshul vyas
2 min readSep 2, 2018

--

Change in the state of the object is termed as event. In more concrete terms, an event is an action or occurrence recognised by software, often originating asynchronously from the external environment, that may be handled by the software. Event is an encapsulation of action and the contextual variables triggering the action, thus often serves as abbreviation for “Execution Variable Encapsulating Named Trigger”.

What is Event Handling ?

An event is generally triggered by a particular type of user interaction, maybe a mouse click, or entering a key through keyboard, or hovering of mouse on the screen. Event Handling is a mechanism that controls the occurrence of a particular event, and the subsequent actions that follow. The Observer Pattern is a paradigm that is implemented across various popular programming languages for event handling.

The Observer Pattern

This pattern has two interacting entities namely the subject, which maintains a list of its dependents, called the observers, and notifies them automatically of any state changes, usually by calling one of their methods.

The Observer pattern addresses the following problems:

  • A one-to-many dependency between objects should be defined without making the objects tightly coupled.
  • It should be ensured that when one object changes state an open-ended number of dependent objects are updated automatically.
  • It should be possible that one object can notify an open-ended number of other objects.
The Observer Pattern example.

The sole responsibility of a subject is to maintain a list of observers and to notify them of state changes by calling their update() operation.
The responsibility of observers is to register /unregister themselves on a subject (to get notified of state changes) and to update their state (synchronise their state with subject's state) when they are notified.

This mechanism makes subjects and observers loosely coupled, with either of them having no explicit knowledge of each other. Observers can be added and removed independently at run-time. This notification-registration interaction is also known as publish-subscribe.

A typical example of this pattern is seen in the bidding auctions. Each bidder possesses a numbered paddle that is used to indicate a bid. The auctioneer starts the bidding, and “observes” when a paddle is raised to accept the bid. The acceptance of the bid changes the bid price which is broadcast to all of the bidders in the form of a new bid. This design typically adheres the Hollywood Principle.

Towards the end it’s all about “Don’t call us, We Call You”

--

--

Anshul vyas
Anshul vyas

Written by Anshul vyas

Product Engineer @ GO-JEK Tech | History and Literature Enthusiast |IIITIAN | Nerd | Music Lover |

No responses yet