C # Support for Observer (OBSERVER) mode (ii)

Source: Internet
Author: User
Tags mailmessage
. NET defines the standard pattern for our application of events, and we should follow the rules for defining events in the application process. A standard event pattern consists of four areas of content:

1. An event information class that inherits from the System.EventArgs type, and the name of the class ends with EventArgs, such as Sendmaileventargs, which is responsible for saving the information sent by the event source to the listener of the event. If the event source does not need to pass additional information to the event listener, you can use Eventargs.empty directly, so we don't have to define our own event information class anymore.

2, the delegate used to define the event (similar to the abstract topic in the Observer pattern subject and abstract observer observer), this delegate return type is void, there are two parameters, the first parameter type is object, The second is System.EventArgs or its subclass, and the name should end with EventHandler, such as:

public delegate void Xxxeventhandler (Object Sender,sendmaileventargs e);. The NET Framework defines a generic delegate for us that conforms to the event specification

System.eventhandler<teventargs> (Object sender, Teventargs e) where Teventargs:eventargs;

So that we do not have to implement a custom delegate in the actual application, the direct use of this generic delegate to meet the needs of our application events.

3. An event source class responsible for notifying event subscribers (similar to a specific topic in the Observer Pattern ConcreteSubject), which contains an event member that is responsible for providing out-of-the-box subscription and logoff interfaces for events and saving their state A method that is responsible for raising an event to notify the subscriber of all this event, which needs to be a protected virtual method, and ends with an event name at the beginning, and accepts a parameter of type System.EventArgs (or subclass). If you define an event member:

public event eventhandler<sendmaileventargs> SendMail;

Then this method should be:

PRotected virtual void Onsendmail (Sendmaileventargs e) {};

There is also a method for converting an external call or input into a desired event, which is called a trigger.

This method is responsible for instantiating an event message class, calling the method onxxx that raised the event, and passing the event message instance past.

4, the Event listener class (similar to the observer pattern of the specific observer concreteobserver), used to listen to the event source messages, this class is used to define the event-compatible method, the format of the return value is void, there are two parameters, the first parameter type is object, The second one is the corresponding event message class. Example: protected void Phone_sendmail (Object sender,eventargs e) ... ;

The following rewrite the mail delivery system in the previous article:

First, define an event message class, which is responsible for saving messages sent to the device.

View Plaincopy to Clipboardprint?
public class Sendmaileventargs:eventargs
{
Read-Only information fields
Public ReadOnly string Message;
Public Sendmaileventargs (String message)
{
This. message = message;
}
}
public class Sendmaileventargs:eventargs
{
Read-Only information fields
Public ReadOnly string Message;
Public Sendmaileventargs (String message)
{
This. message = message;
}
}

The event source class.

View Plaincopy to Clipboardprint?
Class Mailmanager
{
Mail
Public System.Net.Mail.MailMessage mailmess
{
Set
{
mailmess = value;
}
Get
{
return new System.Net.Mail.MailMessage ();
}
}
Defining an event with a generic delegate provided by the. NET Framework
public event eventhandler<sendmaileventargs> SendMail;
The method responsible for raising the event
protected virtual void onsendmial (Sendmaileventargs e)
{
Eventhandler<sendmaileventargs> Sendmail=sendmail;
if (sendMail! = null)
{
Notify all Subscribers
SendMail (this, e);
}
}
Responsible for converting an external call into an event
public void Sendtomail ()
{
if (String.IsNullOrEmpty (mailmess.subject) | | | String. IsNullOrEmpty (Mailmess.body))
{
Console.WriteLine ("Mail Send failed! ");
}
Else
{
Console.WriteLine ("Send mail: {0}", Mailmess.subject);
Instantiate an event information class with the subject of a message
Sendmaileventargs Sendmaileventargs = new Sendmaileventargs (mailmess.subject);
Notify all event Subscribers
This. Onsendmial (Sendmaileventargs);
}
}
}
Class Mailmanager
{
Mail
Public System.Net.Mail.MailMessage mailmess
{
Set
{
mailmess = value;
}
Get
{
return new System.Net.Mail.MailMessage ();
}
}
Defining an event with a generic delegate provided by the. NET Framework
public event eventhandler<sendmaileventargs> SendMail;
The method responsible for raising the event
protected virtual void onsendmial (Sendmaileventargs e)
{
Eventhandler<sendmaileventargs> Sendmail=sendmail;
if (sendMail! = null)
{
Notify all Subscribers
SendMail (this, e);
}
}
Responsible for converting an external call into an event
public void Sendtomail ()
{
if (String.IsNullOrEmpty (mailmess.subject) | | | String. IsNullOrEmpty (Mailmess.body))
{
Console.WriteLine ("Mail Send failed! ");
}
Else
{
Console.WriteLine ("Send mail: {0}", Mailmess.subject);
Instantiate an event information class with the subject of a message
Sendmaileventargs Sendmaileventargs = new Sendmaileventargs (mailmess.subject);
Notify all event Subscribers
This. Onsendmial (Sendmaileventargs);
}
}
}

Event Listener Class

View Plaincopy to Clipboardprint?
Class Mobilephone
{
#region Sendhandler Members

public void SendMessage (Object Sender,sendmaileventargs e)
{
Console.WriteLine ("Mobile Information: {0}", e.message);
}

#endregion
}
public class RTX
{
#region Sendhandler Members

public void SendMessage (Object Sender,sendmaileventargs e)
{
Console.WriteLine ("Rtx information: {0}", e.message);
}

#endregion
}
Class Mobilephone
{
#region Sendhandler Members

public void SendMessage (Object Sender,sendmaileventargs e)
{
Console.WriteLine ("Mobile Information: {0}", e.message);
}

#endregion
}
public class RTX
{
#region Sendhandler Members

public void SendMessage (Object Sender,sendmaileventargs e)
{
Console.WriteLine ("Rtx information: {0}", e.message);
}

#endregion
}



The following is a call from the client

View Plaincopy to Clipboardprint?
Class Program
{
static void Main (string[] args)
{
Event Listener Source Instance
Mailmanager Mailmanager = new Mailmanager ();
Add topics and content to mail
MailManager.MailMess.Subject = "Notification";
MailManager.MailMess.Body = "Observer mode learning." ";
Mailmanager.sendmail+=new Mobilephone (). sendmessage;//Registered mobile Phone notification
Mailmanager.sendmail+=new RTX (). sendmessage;//Register for RTX notification
Mailmanager.sendtomail ();//Send mail
Console.WriteLine ("Press any key to continue ...");
Console.readkey ();
}
}

The above is the C # on the Observer (Observer) mode of support (ii) content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.