A typical Event definition using Microsoft's recommended design pattern.

//This article is picked up from <<Microsoft .NET Framework Programming>> written by Jeffrey Richter.

1. Scenario Description

Suppose you want to design an e-mail application. when an e-mail message arrives, the user might like the message to be forwaded to a fax machine or a pager.

 

2. Designing a type that exposes an event

Code

 here is the screenshot when execution:

A typical Event definition using Microsoft's recommended design pattern.

 

3.  The recommended procedure to define an Event in your own class. 

step1:

Define a tupe that will hold any additional information that shold be sent to receivers of the event notification. By convention, types that hold event information are derived from System.EventArgs, and the name of the type should end with EventArgs.  As you can see, this type is nothing to write home about. It simply serves as a base type from which other types can derive. Many  events don't have any additional information to pass on. For example, when a Button notifies its regisered receivers that the button has been clicked, just invoking the callback method is enough information. When you're defining an event that doesn't have any additional data to pass on, just use EventArgs.Empty rather than constructing a new EventAgrs object.

step2:      

Define a delegate type specifying the prototype of the method that will be called when the event fires. By convention, the name of the delegate shold end with EventHandler. Also by convention, the prototype should have a void return and take two parameters. The first parameter is an object that refers to the object sending the notification, and the second parameter is an EventAgrs-derived type containing any additional information that receivers of the notification require.

If you're defining an event that has no additional information that you want to pass to receivers of the event, you don't have to define a new delegate; you can use the FCL's System.EventHandler delegate and pass EventArgs.Empty for the second parameter. The prototype of EventHandler is as follows:

public delegate void EventHander(object sender, EventArgs e);     

step3:

Define an event

In the previous example, MailMsg is the name of the event. This event is of the MailMsgEventHandler type, meaning that all receivers of the event notification must supply a callback method whose prototype matches that of the MailMsgEventHandler delegate.

step4:

Define a protected, virtual method responsible for notifying registered object of the event.  The OnMailMsg method is called when a new e-mail message arrives. This method receives an intialized MailMsgEventArgs object containing additional information about the event. This method must first check to see whether any objects have regeistered interest in the event, and if they have , fire the event.

A type that uses MailManager as a base type is free to override the OnMailMsg method. This capacity gives the derived type control over the firing of the event. The derived type can handle the new e-mail message in any way it sees fits. Usually, a derived type calls the base type's OnMailMsg method so that the registered object receives the notification. However, the derived type might decide not to have the event forwarded on.

step5:

Define a method that translates the input into the desired event.  Your type must have some method that takes some input and translates it into the firing of an event. In this example, the SimilateArrivingMsg method is called to indicate that a new e-mail message has arrived into MailManager.SimulateArrivingMsg accetps information about the message and constructs a new MailMsgEventArgs object, passing the message information to its constructor. MailManager's own virtual OnMailMsg method is then called to formally notify the MailManager object of the new e-mail message. Normally, this causes the event to be fired, notifying all the registered objects. (As mentioned before, a type using MailManager as a base type can override this behaviour).

你可能感兴趣的:(design pattern)