事件(event)设计准则(二)

 

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"));
        }

    }

一个程序用两种方式写

没有按准则写

      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();
        }

    }

按准则写: 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值