Event-driven programming

An event ( engl. event) is used in software engineering - in development after the event- based programming paradigm - to control the program flow. The program is not run linearly, but there are specific event handlers (English listener, observer, event handler) always executed when a specific event occurs. Event-driven programming is one of the parallel programming techniques, so has their advantages and disadvantages.

Motivation

In programs a common case that resource requirements on a non- deterministic time needs to be serviced (eg, memory allocation or hardware access ) can occur spontaneously or events (eg, user - click). Last finite reason for this non-determinism, is that these events are not under the control of the program, so can not be predicted by the programmer or by the program at runtime. With conventional programming, with a single sequential flow of control that is "waiting " in the occurrence of the event ( for example, the successful resource allocation ) an active, waiting observing that so-called polling performed.

Polling has some disadvantageous properties: since the program execution is stopped until the event occurs, the program efficiency is poor and non- deterministic. Also can not be responded to other events, that is, these may be lost. The efficiency of polling is bad, it is repeatedly performed an unnecessary action, to check whether the event has now occurred.

Event-Driven Programming

One approach to solve this problem efficiently situation, the event -oriented programming, which is based on an inversion of control. That it is no longer maintained in the main control flow on the occurrence of the event ( has the main control flow control ), but the event is a separate control flow associated with ( often implemented as a thread ), which is active when the event occurs independently and will affect the main control flow can take. (see Parallel Programming )

Technical implementations of this idea since the 1960s, the callback function ( with event -specific subroutine) and (hardware ) interrupts, which avoid the disadvantages of synchronization, but this inevitably cause the potential problems of parallel programming.

Can be described event handling techniques in design pattern terminology, as observers ( Observer ).

Use

The concept of event -oriented programming is also well suited for the implementation of graphical user interfaces, in which case the events are usually user actions, such as pressing a key or the click of a button. Another important field of application is computer simulations, which are structured so that state changes are only triggered by events, and in turn, trigger events (see event simulation ).

Event-driven programming fits well with the concepts of object-oriented programming ( OOP) combine: Objects then define not only the properties and methods, but are also sources of the event and provide the opportunity to influence the event handling. The event handlers ( engl. event handler, Germanized the event handler, such as " event processor" or " event handler ") and the events themselves are then modeled as objects. However, it can be argued that the idea of the decoupled messaging between object entities, event -oriented programming has always implicitly been a part concept of OOP.

Events can call depending on the programming environment either an event handler (such as Object Pascal ), or any number of event handlers (like the signal - slot concept ).

There is a possibility, an event "processed" to be marked as ( consume ). The following event handler can query this and then refrain from further processing.

Examples

Example to MS Access

The user can design forms and reports, inter alia, each contained in fields. In addition, there are ' areas ' as the form header, report header, group header and group footer (depending on group level) and the details pane, which itself in turn contain individual fields. All of these terms are objects.

The processing of objects is divided into functional entities whose execution ( " event-driven " ) is the occurrence of certain events depending on:

In forms the events occur primarily through actions on the user interface: mouse actions, petitions, etc. - which are recognized and handled by the Access Engine. Possible events are (for example):

  • For forms: open, view, before entering, as amended, Delete, Close ...
  • On form input fields: if changed, when touching with mouse pointer, when clicked, when you double, with key UP
  • With command buttons: Go there when, when you click, when you double-

For reports, the engine initiates the events data-dependent, similar to the control principles of the standardized programming. Possible events are (for example):

  • For the entire report: When opening / closing, at top, with empty data
  • Report for areas such as group header and footer: when printing, formatting

In addition to the standard processing each event type by MS Access, the programmer for each object and for each event type, specify whether something individual to do - and what. For example, a particular test can be performed after changing an input field; when opening a report in the case of ' empty data ', an error indication will be displayed; a group footer can be made with only one single line per group 'invisible'; Data fields can be visible / invisible or output with certain content.

For such additional functions sets the programmer, possibly from a software "wizards" support, a procedure of specifying, for each object and each event type, a suitable code is stored ( in VBA ); see graphic example. When the event occurs, the appropriate procedure is executed. If no procedure is created, the event is processed in the defined basic form, or there is no processing (eg when you touch the object with the mouse pointer).

Implementation of an event system

The following pseudo code is to show a simple implementation of a system event:

Click = new Event Klick.listener.add ( rain noise ) Klick.listener.add ( rain image ) Click ( ) This simple event system provides a linear running event handling and enables the logging in and out of the event handlers. For parallel execution, the so-called W3C Web Workers plans. The event system shown can be used as follows:

Form = function ( ) {    this.abschicken = new Event ();    ⋮ }   zumServerSenden function () {    ⋮ }   Thank function word () {    alert (" Thank you for completing the form. "); }   var survey = new Form (); umfrage.abschicken.addListener (this, " zumServerSenden "); umfrage.abschicken.addListener (this, " say thank you "); umfrage.abschicken (); see also

311499
de