⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mailmanager.cs

📁 Microsoft.NET.框架程序设计修订版中的书中源码
💻 CS
字号:
/******************************************************************************
Module:  MailManager.cs
Notices: Copyright (c) 2002 Jeffrey Richter
******************************************************************************/


using System;


///////////////////////////////////////////////////////////////////////////////


class MailManager {
   public class ProcessMailMsgEventArgs : EventArgs {

      // 1. Type defining information passed to receivers of the event
      public ProcessMailMsgEventArgs(
         String from, String to, String subject, String body) {

         this.from    = from;
         this.to      = to;
         this.subject = subject;
         this.body    = body;
      }

      public readonly String from, to, subject, body;
   }

   // 2. Delegate type defining the prototype of the callback method
   //    that receivers must implement
   public delegate void ProcessMailMsgEventHandler(
      Object sender, ProcessMailMsgEventArgs args);

   // 3. The event itself
   public event ProcessMailMsgEventHandler ProcessMailMsg;

   // 4. Protected, virtual method responsible for notifying registered
   //    objects of the event
   protected virtual void OnProcessMailMsg(ProcessMailMsgEventArgs e) {

      // Has any objects registered interest with our event? 
      if (ProcessMailMsg != null) {

         // Yes, notify all the objects
         ProcessMailMsg(this, e);
      }
   }

   // 5. Method that translates the input into the desired event
   //    This method is called when a new e-mail message arrives
   public void SimulateArrivingMsg(String from, String to,
      String subject, String body) {

      // Construct an object to hold the information we wish
      // to pass to the receivers of our notification
      ProcessMailMsgEventArgs e = 
         new ProcessMailMsgEventArgs(from, to, subject, body);

      // Call our virtual method notifying our object that the event
      // occurred. If no type overrides this method, our object will
      // notify all the objects that registered interest in the event
      OnProcessMailMsg(e);
   }
}


///////////////////////////////////////////////////////////////////////////////


class Fax {
   // Pass the MailManager object to the constructor
   public Fax(MailManager mm) {

      // Construct an instance of the ProcessMailMsgEventHandler 
      // delegate that refers to our FaxMsg callback method.
      // Register our callback with MailManager's ProcessMailMsg event
      mm.ProcessMailMsg += 
         new MailManager.ProcessMailMsgEventHandler(FaxMsg);
   }

   // This is the method that the MailManager will call
   // when a new e-mail message arrives
   private void FaxMsg(
      Object sender, MailManager.ProcessMailMsgEventArgs e) {

      // 'sender' identifies the MailManager in case 
      // we want to communicate back to it.

      // 'e' identifies the additional event information 
      // that the MailManager wants to give us.

      // Normally, the code here would fax the e-mail message.
      // This test implementation displays the info on the console
      Console.WriteLine("Faxing mail message:");
      Console.WriteLine(
         "   To: {0}\n   From: {1}\n   Subject: {2}\n   Body: {3}\n",
         e.from, e.to, e.subject, e.body);
   }

   public void Unregister(MailManager mm) {

      // Construct an instance of the ProcessMailMsgEventHandler 
      // delegate that refers to our FaxMsg callback method.
      MailManager.ProcessMailMsgEventHandler callback = 
         new MailManager.ProcessMailMsgEventHandler(FaxMsg);

      // Unregister ourself with MailManager's ProcessMailMsg event
      mm.ProcessMailMsg -= callback;
   }
}


///////////////////////////////////////////////////////////////////////////////


class Pager {
   // Pass the MailManager object to the constructor
   public Pager(MailManager mm) {

      // Construct an instance of the ProcessMailMsgEventHandler 
      // delegate that refers to our SendMsgToPager callback method.
      // Register our callback with MailManager's ProcessMailMsg event
      mm.ProcessMailMsg += 
         new MailManager.ProcessMailMsgEventHandler(SendMsgToPager);
   }

   // This is the method that the MailManager will call
   // when a new e-mail message arrives
   private void SendMsgToPager(
      Object sender, MailManager.ProcessMailMsgEventArgs e) {

      // 'sender' identifies the MailManager in case 
      // we want to communicate back to it.

      // 'e' identifies the additional event information 
      // that the MailManager wants to give us.

      // Normally, the code here would send the e-mail message to a pager.
      // This test implementation displays the info on the console
      Console.WriteLine("Sending mail message to pager:");
      Console.WriteLine(
         "   To: {0}\n   From: {1}\n   Subject: {2}\n   Body: {3}\n",
         e.from, e.to, e.subject, e.body);
   }
}


///////////////////////////////////////////////////////////////////////////////


class App {
   static void Main() {
      // Construct a MailManager object
      MailManager mm = new MailManager();

      // Construct a Fax object passing it the MailManager object
      Fax fax = new Fax(mm);

      // Construct a Pager object passing it the MailManager object
      Pager pager = new Pager(mm);

      // Simulate an incoming mail message
      mm.SimulateArrivingMsg("Jeffrey Richter", 
         "Santa", 
         "Christmas", 
         "Thanks for the great presents last year");

      // Force the Fax object to unregister itself with the MailManager
      fax.Unregister(mm);

      // Simulate an incoming mail message
      mm.SimulateArrivingMsg("Jeffrey Richter", "Mom & Dad", 
         "My birthday", 
         "Thanks for the great presents last year");
   }
}


//////////////////////////////// End of File //////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -