事件与委托 echo

客户端代码:

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace EventDemo
 7 {
 8     class Program
 9     {
10         static void Main(string[] args)
11         {
12             //创建一个出版社
13             Publisher publisher = new Publisher("清华大学出版社");
14 
15             //创建两个订阅者
16             Subscripter nicholas = new Subscripter("Nicholas");
17             Subscripter eric = new Subscripter("Eric");
18 
19             //注册事件 (添加委托链)
20             publisher.PubComputer += new Publisher.PubComputerEventHandler(nicholas.Receive);
21             publisher.PubLive += new Publisher.PubLiveEventHandler(nicholas.Receive);         
22             publisher.PubLive += new Publisher.PubLiveEventHandler(eric.Receive);
23 
24             //触发发行动作
25             publisher.IssueComputer("《Begining C# Editon 7》", DateTime.Parse("2010-10-01"), "99.90$");
26             publisher.IssueLive("《Our Life》", DateTime.Parse("2010-10-01"), "18.00$");
27 
28             Console.WriteLine("Eric由于订阅金额不足,不予订阅!\r\n");
29             publisher.PubLive -= new Publisher.PubLiveEventHandler(eric.Receive);
30 
31             publisher.IssueComputer("《Begining C# Editon 7》", DateTime.Parse("2012-03-01"), "99.90$");
32             publisher.IssueLive("《Our Life》", DateTime.Parse("2012-03-01"), "18.00$");
33 
34             Console.ReadKey();
35         }
36     }
37 }
38 
39 /* ======编写事件、委托程序步骤=====
40  * 
41  * 1、确定事件源(事件发送者)和事件接受者(订阅者)
42  * 2、在事件源中声明与该事件所关联的delegate委托,接着声明该事件,类型为委托类型
43  * 3、在事件源中编写触发事件的方法,判断该事件是否为null(是否注册事件),若不为null,则执行该事件
44  * 4、编写一个类,该类继承EventArgs,在该类中定义事件所需的一些参数
45  * 5、在接受者中编写事件处理程序(方法),该方法包含两个参数(object sender, XXXEventArgs e),即与之前定义的委托签名一致
46  * 6、在客户端注册事件(委托链+=)
47  * 7、在客户端调用触发事件的方法
48  * */

事件源(事件发布者):

View Code
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 
  6 namespace EventDemo
  7 {
  8     /// <summary>
  9     /// 出版社
 10     /// </summary>
 11     public class Publisher
 12     {
 13         private string publisherName;
 14 
 15         /// <summary>
 16         /// 出版社名称
 17         /// </summary>
 18         public string PublisherName
 19         {
 20             get { return publisherName; }
 21             set { publisherName = value; }
 22         }
 23 
 24         /// <summary>
 25         /// Constructor
 26         /// </summary>
 27         /// <param name="publisherName">出版社名称</param>
 28         public Publisher(string publisherName)
 29         {
 30             this.publisherName = publisherName;
 31         }
 32 
 33         /// <summary>
 34         /// 发行计算机杂志delegate
 35         /// </summary>
 36         /// <param name="sender"></param>
 37         /// <param name="e"></param>
 38         public delegate void PubComputerEventHandler(object sender, PubEventArgs e);
 39 
 40         /// <summary>
 41         /// 发行计算机杂志event
 42         /// </summary>
 43         public event PubComputerEventHandler PubComputer;
 44 
 45         /// <summary>
 46         /// 发行生活杂志delegate
 47         /// </summary>
 48         /// <param name="sender"></param>
 49         /// <param name="e"></param>
 50         public delegate void PubLiveEventHandler(object sender, PubEventArgs e);
 51 
 52         /// <summary>
 53         /// 发行生活杂志event
 54         /// </summary>
 55         public event PubLiveEventHandler PubLive;
 56 
 57         /// <summary>
 58         /// 出版社发布计算机杂志方法
 59         /// <param name="e">发生事件时的参数</param>
 60         /// </summary>
 61         protected virtual void OnPubComputer(PubEventArgs e)
 62         {
 63             //PubComputerEventHandler delHandler = this.PubComputer;
 64             if (this.PubComputer != null)
 65             {
 66                 this.PubComputer(this, e);
 67             }
 68             else
 69             {
 70                 Console.WriteLine("该杂志没有用户订阅");
 71             }
 72         }
 73 
 74         /// <summary>
 75         /// 出版社发布生活杂志方法
 76         /// <param name="e">发生事件时的参数</param>
 77         /// </summary>
 78         protected virtual void OnPubLive(PubEventArgs e)
 79         {
 80             //PubLiveEventHandler delHandler = this.PubLive;
 81             if (this.PubLive != null)
 82             {
 83                 this.PubLive(this, e);
 84             }
 85             else
 86             {
 87                 Console.WriteLine("该杂志没有用户订阅");
 88             }
 89         }
 90                      
 91         /// <summary>
 92         ///  触发计算机杂志事件
 93         /// </summary>
 94         /// <param name="name">杂志名称</param>
 95         /// <param name="issueTime">发行时间</param>
 96         /// <param name="price">定价</param>
 97         public void IssueComputer(string name, DateTime issueTime, string price)
 98         {
 99             Console.WriteLine("出版社发行:" + name + " 杂志");
100             this.OnPubComputer(new PubEventArgs(name, issueTime, price));
101         }
102 
103         /// <summary>
104         ///  触发计生活杂志事件
105         /// </summary>
106         /// <param name="name">杂志名称</param>
107         /// <param name="issueTime">发行时间</param>
108         /// <param name="price">定价</param>
109         public void IssueLive(string name, DateTime issueTime, string price)
110         {
111             Console.WriteLine("出版社发行:" + name + " 杂志");
112             this.OnPubLive(new PubEventArgs(name, issueTime, price));
113         }
114     }
115 
116     /// <summary>
117     /// 发生事件是的参数
118     /// </summary>
119     public class PubEventArgs : EventArgs
120     {
121         private readonly string pubName;
122         private readonly DateTime pubDateTime;
123         private readonly string price;
124 
125         /// <summary>
126         /// Constructor
127         /// </summary>
128         /// <param name="pubName">发行名称</param>
129         /// <param name="pubDateTime">发行时间</param>
130         /// <param name="price">定价</param>
131         public PubEventArgs(string pubName, DateTime pubDateTime, string price)
132         {
133             this.pubName = pubName;
134             this.pubDateTime = pubDateTime;
135             this.price = price;
136         }
137 
138         /// <summary>
139         /// 杂志的名称
140         /// </summary>
141         public string PubName
142         {
143             get { return pubName; }
144         }
145 
146         /// <summary>
147         /// 发行的日期
148         /// </summary>
149         public DateTime PubDateTime
150         {
151             get { return pubDateTime; }
152         }
153 
154         /// <summary>
155         /// 该杂志的定价
156         /// </summary>
157         public string Price
158         {
159             get { return price; }
160         } 
161     }
162 }


事件订阅者:

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 
 6 namespace EventDemo
 7 {
 8     /// <summary>
 9     /// 杂志订阅者
10     /// </summary>
11     public class Subscripter
12     {
13         private string name;
14 
15         /// <summary>
16         /// Constructor
17         /// </summary>
18         /// <param name="name">the full name of subscripter</param>
19         public Subscripter(string name)
20         {
21             this.name = name;
22         }
23 
24         /// <summary>
25         /// 杂志订阅者的事件处理
26         /// </summary>
27         /// <param name="sender"></param>
28         /// <param name="e"></param>
29         public void Receive(object sender, PubEventArgs e)
30         {
31             Publisher pub = (Publisher)sender;
32 
33             Console.WriteLine("{0} 已经收到了由{1}" + "发行的杂志:{2},\r\n发行日期:{3},\r\n" + "定价:{4}\r\n",
34                 name, pub.PublisherName, e.PubName, e.PubDateTime.ToShortDateString(), e.Price);
35             Console.WriteLine();
36         }
37     }
38 }

执行结果:

原文地址:https://www.cnblogs.com/mangonic/p/2540992.html