Good Example Telling Event and Delegate
2010-03-26 00:30
363 查看
// from <Practical .Net2 and C#2 Harness the Platform , the language, the LFramework>
//Observer and Subscribers Pattern
using System;
class Subscriber{
private string m_name;
public Subscriber(string name){m_name=name;}
//Method to call whenan event is triggered
//i.d when a new report is published
public void OnReceivingInfo(object sender ,EventArgs e){
NewEventArgs info= e as NewEventArgs;
if info !=nulll {
Console.WriteLine(m_name +" receive the new: "+ ((NewEventArgs)e).GetBulletinInf());
}
}
}
//An instance of this class represents a new report
class NewEventArgs:EventArgs{
private stringm_Reprt;
public string GetBulletinInfo() {retrun m_Report;}
public NewsEventArgs(string report){m_Report=report;}
}
//The delegate class. Its instances reference subscriber methods.
public delegate void NewsEventHandler(object sender ,EventArgs e);
//The class whch publishes new reports by triggering events.
class NewsBoard{
//events used to publish news
public event NewsEventHandler USANews;
public event NewsEventHandler WorldNews;
//Methods used to trigger news publishing from outside NewsBoard.
public void OnUSANews(NewsEventArgs newReport){
if (USANews!=null)
USANews(this,newReport);
}
public void OnWorldNews(NewsEventArgs newReport){
if (WorldNews!=null)
WorldNews(this,NewReport);
}
}
class Proogram
{
public static void Main(){
NewsBoard newBord=new NewsBoard();
Subscriber bob=new Subcriber("Bob");
Subscriber joe=new Subcriber("Joe");
Subscriber Max=new Subcriber("Max");
//link Subscribers and the new board
newBoard.USANews +=bob.OnReceivingInfo;
newBoard.USANews +=joe.OnReceivingInfo;
newBoard.WordNews +=joe.OnReceivingInfo;
newBoard.WordNews +=max.OnReceivingInfo;
//public news reports by triggering events
newBoard.OnUSANews(new NewsEventArgs("Oil price increase."));
newBoard.OnWorldNews(new NewsEventArgs("New Election in France."));
//unsubscribe Joe.
newBoard.OnUSANews -= new NewsEventArgs(joe.OnReceivingInfo));
newBoard.OnUSANews(new NewsEventArgs("Hurricane alert."));
}
}
//Observer and Subscribers Pattern
using System;
class Subscriber{
private string m_name;
public Subscriber(string name){m_name=name;}
//Method to call whenan event is triggered
//i.d when a new report is published
public void OnReceivingInfo(object sender ,EventArgs e){
NewEventArgs info= e as NewEventArgs;
if info !=nulll {
Console.WriteLine(m_name +" receive the new: "+ ((NewEventArgs)e).GetBulletinInf());
}
}
}
//An instance of this class represents a new report
class NewEventArgs:EventArgs{
private stringm_Reprt;
public string GetBulletinInfo() {retrun m_Report;}
public NewsEventArgs(string report){m_Report=report;}
}
//The delegate class. Its instances reference subscriber methods.
public delegate void NewsEventHandler(object sender ,EventArgs e);
//The class whch publishes new reports by triggering events.
class NewsBoard{
//events used to publish news
public event NewsEventHandler USANews;
public event NewsEventHandler WorldNews;
//Methods used to trigger news publishing from outside NewsBoard.
public void OnUSANews(NewsEventArgs newReport){
if (USANews!=null)
USANews(this,newReport);
}
public void OnWorldNews(NewsEventArgs newReport){
if (WorldNews!=null)
WorldNews(this,NewReport);
}
}
class Proogram
{
public static void Main(){
NewsBoard newBord=new NewsBoard();
Subscriber bob=new Subcriber("Bob");
Subscriber joe=new Subcriber("Joe");
Subscriber Max=new Subcriber("Max");
//link Subscribers and the new board
newBoard.USANews +=bob.OnReceivingInfo;
newBoard.USANews +=joe.OnReceivingInfo;
newBoard.WordNews +=joe.OnReceivingInfo;
newBoard.WordNews +=max.OnReceivingInfo;
//public news reports by triggering events
newBoard.OnUSANews(new NewsEventArgs("Oil price increase."));
newBoard.OnWorldNews(new NewsEventArgs("New Election in France."));
//unsubscribe Joe.
newBoard.OnUSANews -= new NewsEventArgs(joe.OnReceivingInfo));
newBoard.OnUSANews(new NewsEventArgs("Hurricane alert."));
}
}
相关文章推荐
- C#编程利器之四:委托与事件(Delegate and event) (上)
- C#编程利器之四:委托与事件(Delegate and event)
- The relation between event and delegate in visual c#
- DelegateAndEvent应用之回马枪
- Good example to learn WPF style and template.
- Delegate and Event
- delegate and event
- [Note]CLR via C#3-Event and Delegate
- 委托和事件之烧开水:Delegate and Event
- Difference betweeen event and delegate
- Delegate and event
- event and delegate
- C#编程利器之四:委托与事件(Delegate and event) (上)
- C# delegate and event
- Callback with event and delegate over .net remoting
- C#编程利器之四:委托与事件(Delegate and event) (下)
- C#: Delegate and Event
- C# delegate and event 续
- 委托和事件[delegate and event]_C#
- About Delegate and Event