Event (computing) - Delegate Event Model

A very common and very "programmer-friendly" variant is the delegate event model, which is provided by the most popular graphic frameworks.

This model is based on three entities:

  • a control, which is the event source,
  • consumers, also called listeners, that receive the events from the source,
  • interfaces (in the broader meaning of the term) that describe the protocol by which every event is to be communicated.

Furthermore, the model requires that

  • every listener must implement the interface for the event it wants to listen to
  • every listener must register with the source to declare its desire to listen to some particular event
  • every time the source generates an event, it communicates it to the registered listeners, following the protocol of the interface.

C# uses events as special delegates that can only be fired by the class that declares it. This allows for better abstraction. Here's an example:

delegate void Notifier (string sender); class Model { public event Notifier notifyViews; public void Change { ... notifyViews("Model"); } } class View1 { public View1(Model m) { m.notifyViews += new Notifier(this.Update1); } void Update1(string sender) { Console.WriteLine(sender + " was changed during update"); } } class View2 { public View2(Model m) { m.notifyViews += new Notifier(this.Update2); } void Update2(string sender) { Console.WriteLine(sender + " was changed"); } } class Test { static void Main { Model m = new Model; new View1(m); new View2(m); m.Change; } }

Read more about this topic:  Event (computing)

Famous quotes containing the words event and/or model:

    The defiance of established authority, religious and secular, social and political, as a world-wide phenomenon may well one day be accounted the outstanding event of the last decade.
    Hannah Arendt (1906–1975)

    I had a wonderful job. I worked for a big model agency in Manhattan.... When I got on the subway to go to work, it was like traveling into another world. Oh, the shops were beautiful, we had Bergdorf’s, Bendel’s, Bonwit’s, DePinna. The women wore hats and gloves. Another world. At home, it was cooking, cleaning, taking care of the kids, going to PTA, Girl Scouts. But when I got into the office, everything was different, I was different.
    Estelle Shuster (b. c. 1923)