C# · 12月 20, 2021

c#委托事件及其讲解

一定要标明出处,波哥的文章。所有文章都值得一看。这篇是摘抄的大白话之C#事件讲解。委托 

http://www.cnblogs.com/wudiwushen/archive/2010/04/20/1703368.html

例子是小明委托小明去买电影票

c#语法如下:

 

BugTicketEventHandler();

 

 

 

 

 

</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;小明类</span><span style=”color: #0000ff;”&gt;class</span><span style=”color: #000000;”&gt; MrMing{ </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;声明<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>委托,其实就是个“命令”</span> <span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;delegate</span> <span style=”color: #0000ff;”&gt;void</span><span style=”color: #000000;”&gt; BugTicketEventHandler(); </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span> Main(<span style=”color: #0000ff;”&gt;string</span><span style=”color: #000000;”&gt;[] args) { </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;这里就是具体阐述这个命令是干什么的,本例是MrZhang.BuyTicket“小张买车票”</span> BugTicketEventHandler myDelegate = <span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; BugTicketEventHandler(MrZhang.BuyTicket); </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;这时候委托被附上了具体的<a href=”https://www.jb51.cc/tag/fangfa/” target=”_blank” class=”keywords”>方法</a></span>

<span style=”color: #000000;”> myDelegate();
Console.ReadKey();
}
}

 

 

 

</span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span><span style=”color: #000000;”&gt; BuyMovieTicket() { Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;我去,自己泡妞,还要让我带电影票!</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); }}</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;小明类</span><span style=”color: #0000ff;”&gt;class</span><span style=”color: #000000;”&gt; MrMing{ </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;声明<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>委托,其实就是个“命令”</span> <span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;delegate</span> <span style=”color: #0000ff;”&gt;void</span><span style=”color: #000000;”&gt; BugTicketEventHandler(); </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span> Main(<span style=”color: #0000ff;”&gt;string</span><span style=”color: #000000;”&gt;[] args) { </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;这里就是具体阐述这个命令是干什么的,本例是MrZhang.BuyTicket“小张买车票”</span> BugTicketEventHandler myDelegate = <span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; BugTicketEventHandler(MrZhang.BuyTicket); myDelegate </span>+=<span style=”color: #000000;”&gt; MrZhang.BuyMovieTicket; </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;这时候委托被附上了具体的<a href=”https://www.jb51.cc/tag/fangfa/” target=”_blank” class=”keywords”>方法</a></span>

<span style=”color: #000000;”> myDelegate();
Console.ReadKey();
}
}

 

      其实,我们只是在程序中加了

      好了,关于大白话的委托就介绍到这里了,是不是并没有大家想的这么可怕呢,在下一回我们就接着大白话的EVENT的讲解。

http://www.cnblogs.com/wudiwushen/archive/2010/04/20/1703763.html

 

 

 

事件是类在发生其关注的事情时用来提供通知的一种方式。

 

 

事件发行者(Publisher):一个事件的发行者,也称作是发送者(sender),其实就是个对象,这个对象会自行维护本身的状态信息,当本身状态信息变动时,便触发一个事件,并通知说有的事件订阅者。

 

事件订阅者(Subscriber):对事件感兴趣的对象,也称为Receiver,可以注册感兴趣的事件,在事件发行者触发一个事件后,会自动执行这段代码。

 

 

(OnPublish != </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;Subscriber <a href=”https://www.jb51.cc/tag/dingyue/” target=”_blank” class=”keywords”>订阅</a>者,无赖小明</span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;class</span><span style=”color: #000000;”&gt; MrMing{ </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;对事件感兴趣的事情,这里指对出版社的书感兴趣</span> <span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span><span style=”color: #000000;”&gt; Receive() { Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;嘎嘎,我已经收到最新一期的《火影忍者》啦!!</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); }}</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;Subscriber <a href=”https://www.jb51.cc/tag/dingyue/” target=”_blank” class=”keywords”>订阅</a>者,悲情人物小张</span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;class</span><span style=”color: #000000;”&gt; MrZhang{ </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;对事件感兴趣的事情</span> <span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span><span style=”color: #000000;”&gt; Receive() { Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;幼稚,这么大了,还看《火影忍者》,SB小明!</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); }}</span><span style=”color: #0000ff;”&gt;class</span><span style=”color: #000000;”&gt; Story{ </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span> Main(<span style=”color: #0000ff;”&gt;string</span><span style=”color: #000000;”&gt;[] args) { </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;实例化<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>出版社</span> Publisher publisher = <span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; Publisher(); </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;给这个出火影忍者的事件<a href=”https://www.jb51.cc/tag/zhuce/” target=”_blank” class=”keywords”>注册</a>感兴趣的<a href=”https://www.jb51.cc/tag/dingyue/” target=”_blank” class=”keywords”>订阅</a>者,此例中是小明</span> publisher.OnPublish += <span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; Publisher.PublishEventHander(MrMing.Receive); </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;另一种事件<a href=”https://www.jb51.cc/tag/zhuce/” target=”_blank” class=”keywords”>注册</a>方式 </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;publisher.OnPublish += MrMing.Receive; </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;发布者<a href=”https://www.jb51.cc/tag/zaizheli/” target=”_blank” class=”keywords”>在这里</a>触发出版火影忍者的事件</span>

<span style=”color: #000000;”> publisher.issue();

Console.ReadKey(); }}</span></pre>

 

结果如下:

 

 

 

 

 

 

 

委托赋值:

 

 

 

 

        

 

 

今天我接着上面的3篇文章来讲一下,为什么我们在日常的编程活动中遇到这么多sender,EventArgs e 参数:
protected void Page_Load(object sender,EventArgs e)
{

 

}

 

protected void btnSearch_Click(object sender,ImageClickEventArgs e)
{

 

}

 

protected void GrdBill_RowDataBound(object sender,GridViewRowEventArgs e)
{
          
}
那他们到底表示什么呢?

 

在回答上面的问题之前,我们先搞懂 .Net Framework的编码规范:

 

 

一、委托类型的名称都应该以EventHandler结束。
二、委托的原型定义:有一个void返回值,并接受两个输入参数:一个Object 类型,一个 EventArgs类型(或继承自EventArgs)。
三、事件的命名为 委托去掉 EventHandler之后剩余的部分。
四、继承自EventArgs的类型应该以EventArgs结尾。

 

这就是微软编码的规范,当然这不仅仅是规则,而是在这种规则下使程序有更大的灵活性,那我们就继续重构第三讲的例子,让他符合微软的规范。

 

 

} </span><span style=”color: #0000ff;”&gt;public</span> PubEventArgs (<span style=”color: #0000ff;”&gt;string</span><span style=”color: #000000;”&gt; magazineName) { </span><span style=”color: #0000ff;”&gt;this</span>.magazineName =<span style=”color: #000000;”&gt; magazineName; }}</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;发布者(Publiser)</span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;class</span><span style=”color: #000000;”&gt; Publisher{ </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;声明<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>出版的委托 </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;这里多了<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>参数sender,它所代表的就是Subject,也就是监视对象,本例中就是Publisher</span> <span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;delegate</span> <span style=”color: #0000ff;”&gt;void</span> PublishEventHander(<span style=”color: #0000ff;”&gt;object</span><span style=”color: #000000;”&gt; sender,PubEventArgs e); </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;在委托的机制下我们建立以个出版事件</span> <span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;event</span><span style=”color: #000000;”&gt; PublishEventHander Publish; </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;声明<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>可重写的OnPublish的保护<a href=”https://www.jb51.cc/tag/hanshu/” target=”_blank” class=”keywords”>函数</a></span> <span style=”color: #0000ff;”&gt;protected</span> <span style=”color: #0000ff;”&gt;virtual</span> <span style=”color: #0000ff;”&gt;void</span><span style=”color: #000000;”&gt; OnPublish(PubEventArgs e) { </span><span style=”color: #0000ff;”&gt;if</span> (Publish != <span style=”color: #0000ff;”&gt;null</span><span style=”color: #000000;”&gt;) { </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;Sender = this,也就是Publisher</span> <span style=”color: #0000ff;”&gt;this</span>.Publish(<span style=”color: #0000ff;”&gt;this</span><span style=”color: #000000;”&gt;,e); } } </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;事件必须要在<a href=”https://www.jb51.cc/tag/fangfa/” target=”_blank” class=”keywords”>方法</a>里去触发</span> <span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;void</span> issue(<span style=”color: #0000ff;”&gt;string</span><span style=”color: #000000;”&gt; magazineName) { OnPublish(</span><span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; PubEventArgs(magazineName)); }}</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;Subscriber <a href=”https://www.jb51.cc/tag/dingyue/” target=”_blank” class=”keywords”>订阅</a>者</span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;class</span><span style=”color: #000000;”&gt; MrMing{ </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;对事件感兴趣的事情</span> <span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span> Receive(<span style=”color: #0000ff;”&gt;object</span><span style=”color: #000000;”&gt; sender,PubEventArgs e) { Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;嘎嘎,我已经收到最新一期的《</span><span style=”color: #800000;”&gt;”</span>+e.magazineName+<span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;》啦!!</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); }}</span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;class</span><span style=”color: #000000;”&gt; MrZhang{ </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;对事件感兴趣的事情</span> <span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span> Receive(<span style=”color: #0000ff;”&gt;object</span><span style=”color: #000000;”&gt; sender,PubEventArgs e) { Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;幼稚,这么大了,还看《火影忍者》,SB小明!</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;这个我定的《</span><span style=”color: #800000;”&gt;”</span>+e.magazineName+<span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;》,哇哈哈!</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); }}</span><span style=”color: #0000ff;”&gt;class</span><span style=”color: #000000;”&gt; Story{ </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span> Main(<span style=”color: #0000ff;”&gt;string</span><span style=”color: #000000;”&gt;[] args) { </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;实例化<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>出版社</span> Publisher publisher = <span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; Publisher(); Console.Write(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;请输入要发行的杂志:</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); </span><span style=”color: #0000ff;”&gt;string</span> name =<span style=”color: #000000;”&gt; Console.ReadLine(); </span><span style=”color: #0000ff;”&gt;if</span> (name == <span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;火影忍者</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;) { </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;给这个出火影忍者的事件<a href=”https://www.jb51.cc/tag/zhuce/” target=”_blank” class=”keywords”>注册</a>感兴趣的<a href=”https://www.jb51.cc/tag/dingyue/” target=”_blank” class=”keywords”>订阅</a>者,此例中是小明</span> publisher.Publish += <span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; Publisher.PublishEventHander(MrMing.Receive); </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;发布者<a href=”https://www.jb51.cc/tag/zaizheli/” target=”_blank” class=”keywords”>在这里</a>触发出版火影忍者的事件</span> publisher.issue(<span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;火影忍者</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); } </span><span style=”color: #0000ff;”&gt;else</span><span style=”color: #000000;”&gt; { </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;给这个出火影忍者的事件<a href=”https://www.jb51.cc/tag/zhuce/” target=”_blank” class=”keywords”>注册</a>感兴趣的<a href=”https://www.jb51.cc/tag/dingyue/” target=”_blank” class=”keywords”>订阅</a>者,此例中是小明[另一种事件<a href=”https://www.jb51.cc/tag/zhuce/” target=”_blank” class=”keywords”>注册</a>方式]</span> publisher.Publish +=<span style=”color: #000000;”&gt; MrZhang.Receive; publisher.issue(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;环球日报</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); } Console.ReadKey(); }}</span></pre>

 

 

通过例子我再做一次说明,其实我们不用把Sender,e想的过于可怕

 

一、委托声明原型中的Object类型的参数代表了Subject,也就是监视对象,在本例中是 Publisher(出版社)。。
二、EventArgs 对象包含了Observer所感兴趣的数据,在本例中是杂志。

 

好了,我们接着讲我们的委托与事件,其实如果大家对设计模式精通的话,其实他们关联的是观察者(Observer)模式,这里我就不再描述什么是观察者模式了,只是简单讲一下他们的关联:

 

 

如果看不懂的话也没关系,当大家OO达到一定程度了,自然而然就会明白。

 

 

 

最后我们来看一个我们日常最最常用的观察者模式:

 

场景:当我们用信用卡刷完钱的时候,我们就会接收到手机短信,或者是电子邮件,其实这就是Observer pattern

</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;Obverser电子<a href=”https://www.jb51.cc/tag/youjian/” target=”_blank” class=”keywords”>邮件</a>,手机关心的对象e,分别是<a href=”https://www.jb51.cc/tag/youjian/” target=”_blank” class=”keywords”>邮件</a>地址、手机号码、<a href=”https://www.jb51.cc/tag/qukuan/” target=”_blank” class=”keywords”>取款</a>金额</span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;class</span><span style=”color: #000000;”&gt; UserEventArgs : EventArgs{ </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;readonly</span> <span style=”color: #0000ff;”&gt;string</span><span style=”color: #000000;”&gt; emailAddress; </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;readonly</span> <span style=”color: #0000ff;”&gt;string</span><span style=”color: #000000;”&gt; mobilePhone; </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;readonly</span> <span style=”color: #0000ff;”&gt;string</span><span style=”color: #000000;”&gt; amount; </span><span style=”color: #0000ff;”&gt;public</span> UserEventArgs(<span style=”color: #0000ff;”&gt;string</span> emailAddress,<span style=”color: #0000ff;”&gt;string</span> mobilePhone,<span style=”color: #0000ff;”&gt;string</span><span style=”color: #000000;”&gt; amount) { </span><span style=”color: #0000ff;”&gt;this</span>.emailAddress =<span style=”color: #000000;”&gt; emailAddress; </span><span style=”color: #0000ff;”&gt;this</span>.mobilePhone =<span style=”color: #000000;”&gt; mobilePhone; </span><span style=”color: #0000ff;”&gt;this</span>.amount =<span style=”color: #000000;”&gt; amount; }}</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;发布者,也就是被监视的对象-银行账号</span><span style=”color: #0000ff;”&gt;class</span><span style=”color: #000000;”&gt; BankAccount{ </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;声明<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>处理银行交易的委托</span> <span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;delegate</span> <span style=”color: #0000ff;”&gt;void</span> ProcessTranEventHandler(<span style=”color: #0000ff;”&gt;object</span><span style=”color: #000000;”&gt; sender,UserEventArgs e); </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;声明<a href=”https://www.jb51.cc/tag/yige/” target=”_blank” class=”keywords”>一个</a>事件</span> <span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;event</span><span style=”color: #000000;”&gt; ProcessTranEventHandler ProcessTran; </span><span style=”color: #0000ff;”&gt;protected</span> <span style=”color: #0000ff;”&gt;virtual</span> <span style=”color: #0000ff;”&gt;void</span><span style=”color: #000000;”&gt; OnProcessTran(UserEventArgs e) { </span><span style=”color: #0000ff;”&gt;if</span> (ProcessTran != <span style=”color: #0000ff;”&gt;null</span><span style=”color: #000000;”&gt;) { ProcessTran(</span><span style=”color: #0000ff;”&gt;this</span><span style=”color: #000000;”&gt;,e); } } </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;void</span><span style=”color: #000000;”&gt; Prcess(UserEventArgs e) { OnProcessTran(e); }}</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;观察者Email</span><span style=”color: #0000ff;”&gt;class</span><span style=”color: #000000;”&gt; Email{ </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span> SendEmail(<span style=”color: #0000ff;”&gt;object</span><span style=”color: #000000;”&gt; sender,UserEventArgs e) { Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;向<a href=”https://www.jb51.cc/tag/yonghu/” target=”_blank” class=”keywords”>用户</a>邮箱</span><span style=”color: #800000;”&gt;”</span> + e.emailAddress + <span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;<a href=”https://www.jb51.cc/tag/fasongyoujian/” target=”_blank” class=”keywords”>发送邮件</a>:您在</span><span style=”color: #800000;”&gt;”</span>+Sy<a href=”https://www.jb51.cc/tag/stem/” target=”_blank” class=”keywords”>stem</a>.DateTime.<a href=”https://www.jb51.cc/tag/Now/” target=”_blank” class=”keywords”>Now</a>.ToString()+<span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;<a href=”https://www.jb51.cc/tag/qukuan/” target=”_blank” class=”keywords”>取款</a>金额为</span><span style=”color: #800000;”&gt;”</span>+<span style=”color: #000000;”&gt;e.amount); }}</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;观察者手机</span><span style=”color: #0000ff;”&gt;class</span><span style=”color: #000000;”&gt; Mobile{ </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span> SendNotification(<span style=”color: #0000ff;”&gt;object</span><span style=”color: #000000;”&gt; sender,UserEventArgs e) { Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;向<a href=”https://www.jb51.cc/tag/yonghu/” target=”_blank” class=”keywords”>用户</a>手机</span><span style=”color: #800000;”&gt;”</span> + e.mobilePhone + <span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;发送短信:您在</span><span style=”color: #800000;”&gt;”</span> + Sy<a href=”https://www.jb51.cc/tag/stem/” target=”_blank” class=”keywords”>stem</a>.DateTime.<a href=”https://www.jb51.cc/tag/Now/” target=”_blank” class=”keywords”>Now</a>.ToString() + <span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;<a href=”https://www.jb51.cc/tag/qukuan/” target=”_blank” class=”keywords”>取款</a>金额为</span><span style=”color: #800000;”&gt;”</span> +<span style=”color: #000000;”&gt; e.amount); }}</span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;<a href=”https://www.jb51.cc/tag/dingyue/” target=”_blank” class=”keywords”>订阅</a>系统,实现银行系统<a href=”https://www.jb51.cc/tag/dingyue/” target=”_blank” class=”keywords”>订阅</a>几个Observer,实现与客户端的松耦合</span><span style=”color: #0000ff;”&gt;class</span><span style=”color: #000000;”&gt; SubscribSy<a href=”https://www.jb51.cc/tag/stem/” target=”_blank” class=”keywords”>stem</a>{ </span><span style=”color: #0000ff;”&gt;public</span><span style=”color: #000000;”&gt; SubscribSy<a href=”https://www.jb51.cc/tag/stem/” target=”_blank” class=”keywords”>stem</a>() { } </span><span style=”color: #0000ff;”&gt;public</span><span style=”color: #000000;”&gt; SubscribSy<a href=”https://www.jb51.cc/tag/stem/” target=”_blank” class=”keywords”>stem</a>(BankAccount bankAccount,UserEventArgs e) { </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;现在我们在银行账户<a href=”https://www.jb51.cc/tag/dingyue/” target=”_blank” class=”keywords”>订阅</a>2个,分别是电子<a href=”https://www.jb51.cc/tag/youjian/” target=”_blank” class=”keywords”>邮件</a>和手机短信</span> bankAccount.ProcessTran += <span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; BankAccount.ProcessTranEventHandler(Email.SendEmail); bankAccount.ProcessTran </span>+= <span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; BankAccount.ProcessTranEventHandler(Mobile.SendNotification); bankAccount.Prcess(e); }}</span><span style=”color: #0000ff;”&gt;class</span><span style=”color: #000000;”&gt; Client{ </span><span style=”color: #0000ff;”&gt;public</span> <span style=”color: #0000ff;”&gt;static</span> <span style=”color: #0000ff;”&gt;void</span> Main(<span style=”color: #0000ff;”&gt;string</span><span style=”color: #000000;”&gt;[] args) { Console.Write(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;请输入您要<a href=”https://www.jb51.cc/tag/qukuan/” target=”_blank” class=”keywords”>取款</a>的金额:</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); </span><span style=”color: #0000ff;”&gt;string</span> amount =<span style=”color: #000000;”&gt; Console.ReadLine(); Console.WriteLine(</span><span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;交易成功,请取磁卡。</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;); </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;初始化e</span> UserEventArgs user = <span style=”color: #0000ff;”&gt;new</span> UserEventArgs(<span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;jinjiangbo2008@163.com</span><span style=”color: #800000;”&gt;”</span>,<span style=”color: #800000;”&gt;”</span><span style=”color: #800000;”&gt;18868789776</span><span style=”color: #800000;”&gt;”</span><span style=”color: #000000;”&gt;,amount); </span><span style=”color: #008000;”&gt;//</span><span style=”color: #008000;”&gt;初始化<a href=”https://www.jb51.cc/tag/dingyue/” target=”_blank” class=”keywords”>订阅</a>系统</span> SubscribSy<a href=”https://www.jb51.cc/tag/stem/” target=”_blank” class=”keywords”>stem</a> subject = <span style=”color: #0000ff;”&gt;new</span> SubscribSy<a href=”https://www.jb51.cc/tag/stem/” target=”_blank” class=”keywords”>stem</a>(<span style=”color: #0000ff;”&gt;new</span><span style=”color: #000000;”&gt; BankAccount(),user); Console.ReadKey(); }}</span></pre>

运行结果如下:

网上还有个热水器烧水的OBSERVER PATTERN 也是蛮经典的,大家可以看看。