class
PubEventArgs : EventArgs
... {
private readonly string m_magazineName;
private readonly DateTime m_pubDate;
public PubEventArgs(string magazineName, DateTime pubDate)
...{
m_magazineName = magazineName;
m_pubDate = pubDate;
}
public string magazineName
...{
get ...{ return m_magazineName; }
}
public DateTime pubDate
...{
get ...{ return m_pubDate; }
}
}
class Publisher
... {
public delegate void PubComputerEventHandler(object sender,PubEventArgs e);//statement of the event for agents
public delegate void PubLiftEventHandler(object sender, PubEventArgs e);
public event PubComputerEventHandler PubComputer;//event of statement
public event PubLiftEventHandler PubLife;
protected virtual void OnPubComputer(PubEventArgs e)
...{
PubComputerEventHandler handler = PubComputer;
if(handler!=null)
...{
handler(this, e);
}
}
protected virtual void OnPubLife(PubEventArgs e)
...{
PubLiftEventHandler handler = PubLife;
if (handler != null)
...{
handler(this, e);
}
}
public void issueComputer(string magazineName,DateTime pubDate)//method of trigger event
...{
Console.WriteLine("issue "+magazineName);
OnPubComputer(new PubEventArgs(magazineName, pubDate));
}
public void issueLife(string magaineName,DateTime pubDate)
...{
Console.WriteLine("issue " + magaineName);
OnPubLife(new PubEventArgs(magaineName, pubDate));
}
}
class Subscriber
... {
private string name;
public Subscriber(string name)
...{
this.name = name;
}
public void Reveive(object sender ,PubEventArgs e)//define in the event subscribers procedure of handing event
...{
Console.WriteLine(e.pubDate+" "+name + " have already received " + e.magazineName);
}
}
class EventTest
... {
static void Main()
...{
Publisher pub = new Publisher();
Subscriber sam = new Subscriber("Sam");
pub.PubComputer += new Publisher.PubComputerEventHandler(sam.Reveive);//Subscriber to the event issuers an event
Subscriber Jone = new Subscriber("Jone");
pub.PubComputer += new Publisher.PubComputerEventHandler(Jone.Reveive);
pub.PubLife += new Publisher.PubLiftEventHandler(Jone.Reveive);
pub.issueComputer("computer magazine",Convert.ToDateTime("2006-1-1"));//trigger event
pub.issueLife("Life magazine", Convert.ToDateTime("2006-1-1"));
Console.WriteLine();
Console.WriteLine("one year ago");
pub.PubComputer -= new Publisher.PubComputerEventHandler(Jone.Reveive);
pub.issueComputer("computer magazine", Convert.ToDateTime("2007-1-1"));
pub.issueLife("Life magazine", Convert.ToDateTime("2007-1-1"));
}
}
... {
private readonly string m_magazineName;
private readonly DateTime m_pubDate;
public PubEventArgs(string magazineName, DateTime pubDate)
...{
m_magazineName = magazineName;
m_pubDate = pubDate;
}
public string magazineName
...{
get ...{ return m_magazineName; }
}
public DateTime pubDate
...{
get ...{ return m_pubDate; }
}
}
class Publisher
... {
public delegate void PubComputerEventHandler(object sender,PubEventArgs e);//statement of the event for agents
public delegate void PubLiftEventHandler(object sender, PubEventArgs e);
public event PubComputerEventHandler PubComputer;//event of statement
public event PubLiftEventHandler PubLife;
protected virtual void OnPubComputer(PubEventArgs e)
...{
PubComputerEventHandler handler = PubComputer;
if(handler!=null)
...{
handler(this, e);
}
}
protected virtual void OnPubLife(PubEventArgs e)
...{
PubLiftEventHandler handler = PubLife;
if (handler != null)
...{
handler(this, e);
}
}
public void issueComputer(string magazineName,DateTime pubDate)//method of trigger event
...{
Console.WriteLine("issue "+magazineName);
OnPubComputer(new PubEventArgs(magazineName, pubDate));
}
public void issueLife(string magaineName,DateTime pubDate)
...{
Console.WriteLine("issue " + magaineName);
OnPubLife(new PubEventArgs(magaineName, pubDate));
}
}
class Subscriber
... {
private string name;
public Subscriber(string name)
...{
this.name = name;
}
public void Reveive(object sender ,PubEventArgs e)//define in the event subscribers procedure of handing event
...{
Console.WriteLine(e.pubDate+" "+name + " have already received " + e.magazineName);
}
}
class EventTest
... {
static void Main()
...{
Publisher pub = new Publisher();
Subscriber sam = new Subscriber("Sam");
pub.PubComputer += new Publisher.PubComputerEventHandler(sam.Reveive);//Subscriber to the event issuers an event
Subscriber Jone = new Subscriber("Jone");
pub.PubComputer += new Publisher.PubComputerEventHandler(Jone.Reveive);
pub.PubLife += new Publisher.PubLiftEventHandler(Jone.Reveive);
pub.issueComputer("computer magazine",Convert.ToDateTime("2006-1-1"));//trigger event
pub.issueLife("Life magazine", Convert.ToDateTime("2006-1-1"));
Console.WriteLine();
Console.WriteLine("one year ago");
pub.PubComputer -= new Publisher.PubComputerEventHandler(Jone.Reveive);
pub.issueComputer("computer magazine", Convert.ToDateTime("2007-1-1"));
pub.issueLife("Life magazine", Convert.ToDateTime("2007-1-1"));
}
}
一个程序用两种方式写
没有按准则写
class
Publisher
... {
public delegate void PubComputer(string magazineName);//statement of the event for agents
public delegate void PubLift(string magazineName);
public event PubComputer OnPubComputer;//event of statement
public event PubLift OnPubLife;
public void issueComputer()//method of trigger event
...{
if (OnPubComputer != null)
...{
Console.WriteLine("issue <<Programs>> magazine");
OnPubComputer("Computer magazine");
}
}
public void issueLife()
...{
if (OnPubLife != null)
...{
Console.WriteLine("issue <<Life>> magazine");
OnPubLife("Life magazine");
}
}
}
class Subscriber
... {
private string name;
public Subscriber(string name)
...{
this.name = name;
}
public void Reveive(string magazineName)//define in the event subscribers procedure of handing event
...{
Console.WriteLine(name+" have already received "+magazineName);
}
}
class EventTest
... {
static void Main()
...{
Publisher pub = new Publisher();
Subscriber sam = new Subscriber("Sam");
pub.OnPubComputer += new Publisher.PubComputer(sam.Reveive);//Subscriber to the event issuers an event
Subscriber Jone = new Subscriber("Jone");
pub.OnPubComputer += new Publisher.PubComputer(Jone.Reveive);
pub.OnPubLife += new Publisher.PubLift(Jone.Reveive);
pub.issueComputer();//trigger event
pub.issueLife();
Console.WriteLine();
Console.WriteLine("one year ago");
pub.OnPubComputer -= new Publisher.PubComputer(Jone.Reveive);
pub.issueComputer();
pub.issueLife();
}
}
... {
public delegate void PubComputer(string magazineName);//statement of the event for agents
public delegate void PubLift(string magazineName);
public event PubComputer OnPubComputer;//event of statement
public event PubLift OnPubLife;
public void issueComputer()//method of trigger event
...{
if (OnPubComputer != null)
...{
Console.WriteLine("issue <<Programs>> magazine");
OnPubComputer("Computer magazine");
}
}
public void issueLife()
...{
if (OnPubLife != null)
...{
Console.WriteLine("issue <<Life>> magazine");
OnPubLife("Life magazine");
}
}
}
class Subscriber
... {
private string name;
public Subscriber(string name)
...{
this.name = name;
}
public void Reveive(string magazineName)//define in the event subscribers procedure of handing event
...{
Console.WriteLine(name+" have already received "+magazineName);
}
}
class EventTest
... {
static void Main()
...{
Publisher pub = new Publisher();
Subscriber sam = new Subscriber("Sam");
pub.OnPubComputer += new Publisher.PubComputer(sam.Reveive);//Subscriber to the event issuers an event
Subscriber Jone = new Subscriber("Jone");
pub.OnPubComputer += new Publisher.PubComputer(Jone.Reveive);
pub.OnPubLife += new Publisher.PubLift(Jone.Reveive);
pub.issueComputer();//trigger event
pub.issueLife();
Console.WriteLine();
Console.WriteLine("one year ago");
pub.OnPubComputer -= new Publisher.PubComputer(Jone.Reveive);
pub.issueComputer();
pub.issueLife();
}
}
按准则写: