Publishers and Subscribers
One common requirement in many programs is that when a particular program event occurs, other parts of the program need to be notified that the event has occurred.
For example when you click a button there's some action from program.
One pattern for satisfying this requirement is called the publisher/subscriber pattern. In this pattern,a class, called the publisher, defines a set of events that other parts of the program might be interested in. Other classes can then "sign up" to be notified by the publisher when these events occur. These subscriber classes "sign up" for notification by supplying a method to the publisher. When the event occurs, the publisher "raises the event", and all the methods submitted by the subscribers are executed.
The methods supplied by the subscribers are called callback methods, because the publisher "calls the subscribers back" by executing their methods. They are also called event handlers, because they are the code that is called to handle the event. See following figure:
The following are some important terms related to events:
One pattern for satisfying this requirement is called the publisher/subscriber pattern. In this pattern,a class, called the publisher, defines a set of events that other parts of the program might be interested in. Other classes can then "sign up" to be notified by the publisher when these events occur. These subscriber classes "sign up" for notification by supplying a method to the publisher. When the event occurs, the publisher "raises the event", and all the methods submitted by the subscribers are executed.
The methods supplied by the subscribers are called callback methods, because the publisher "calls the subscribers back" by executing their methods. They are also called event handlers, because they are the code that is called to handle the event. See following figure:
The following are some important terms related to events:
- Publisher: A class or struct that publishes an event so that other classes can be notified when the event occurs.
- Subscriber: A class or struct that registers to be notified when the event occurs.
- Event handler: A method that is registered with the publisher, by the subscriber, and is executed when the publisher raises the event.
- Raising an event: The term for invoking or firing an event. When an event is raised, all the methods registered with that event are invoked.
- An event gives structured access to its privately controlled delegate. That is, you can’t directly access the delegate.
- There are fewer operations available than with a delegate. With an event you can only add and remove event handlers, and invoke the event.
- When an event is raised, it invokes the delegate, which sequentially calls the methods in its invocation list.
+=
and -=
operators are sticking out to the left of the event box. This is because they are the only
operations (apart from invoking the event itself) allowed on an event.
Event's Code Components
There are five pieces of code that need to be in place to use events. These pieces of code are the following:
- Delegate type declaration: The event and the event handlers must have a common signature and return type, which is described by a delegate type.
- Event handler declarations: These are the declarations, in the subscriber classes, of the methods to be executed when the event is raised. These do not have to be explicitly named methods; they can also be anonymous methods or lambda expressions.
- Event declaration: The publisher class must declare an event member that subscribers can register with. When a class declares a public event, it is said to have published the event.
- Event registration: The subscribers must register with an event in order to be notified when it has been raised. This is the code that connects the event handlers to the event.
- Code that raises the event: This is the code in the publisher that "fires" the event,causing it to invoke all the event handlers registered with it.
0 comments:
Post a Comment