zoukankan      html  css  js  c++  java
  • .net framework事件设计准则

    代码

    namespace CSDemos.Model
    {
    public delegate void PubEventHandler(object sender,PubEventArgs e);

    public class Publisher
    {
    private string _name;

    public string Name
    {
    get { return _name; }
    set { _name = value; }
    }

    public Publisher(string name) {
    this._name = name;
    }

    public event PubEventHandler PubComputer;
    public event PubEventHandler PubLife;

    //可以让子类重写
    protected virtual void OnPubComputer(PubEventArgs e){ //触发事件
    if (PubComputer != null)
    {
    //以免订阅者事件处理时间过长,发生并发问题。
    PubEventHandler pubComputerTmp = PubComputer;
    pubComputerTmp(
    this, e);
    }
    }

    protected virtual void OnPubLife(PubEventArgs e)
    {
    //触发事件
    if (PubLife != null)
    {
    //以免订阅者事件处理时间过长,发生并发问题。
    PubEventHandler pubLifeTmp = PubLife;
    pubLifeTmp(
    this, e);
    }
    }

    public void DoPubComputer(string magazineName,DateTime dtPubdate) {
    PubEventArgs e
    = new PubEventArgs(magazineName, dtPubdate);
    OnPubComputer(e);
    }


    public void DoPubLife(string magazineName, DateTime dtPubdate)
    {
    PubEventArgs e
    = new PubEventArgs(magazineName, dtPubdate);
    OnPubLife(e);
    }

    }
    }

    订阅者Subscriber类:

    代码
    namespace CSDemos.Model
    {
    class Subscriber
    {
    private string _name;
    public string Name
    {
    get { return _name; }
    set { _name = value; }
    }

    public Subscriber(string name)
    {
    this._name = name;
    }

    public void Receive(object sender,PubEventArgs e)
    {

    MessageBox.Show(
    string.Format("{0}于{1}收到{2}",this.Name,e.PubDate.ToString(),e.Name));

    }
    }
    }

    EventArgs:

    namespace CSDemos.Model
    {
        public  class PubEventArgs:EventArgs
        {
            private string _name;

            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }
            private DateTime _pubDate;

            public DateTime PubDate
            {
                get { return _pubDate; }
                set { _pubDate = value; }
            }

            public PubEventArgs(string name, DateTime dt)
            {
                this._name = name;
                this._pubDate = dt;
            }

        }
    }

    Client调用:

                Publisher pub = new Publisher("新华出版社");


                Subscriber subZs = new Subscriber("张三");
                Subscriber subLs = new Subscriber("李四");

                pub.PubComputer += subZs.Receive;
                pub.PubComputer += subLs.Receive;

                pub.PubLife += subZs.Receive;

                pub.DoPubComputer("电脑杂志", DateTime.Now);
                pub.DoPubLife("生活杂志", DateTime.Now);

  • 相关阅读:
    2018-2019-2 20165235 《网络对抗技术》 Exp6 信息搜集与漏洞扫描
    2018-2019 20165235 网络对抗 Exp5 MSF基础
    2018-2019 20165235 网络对抗 Exp4 恶意代码分析
    2018-2019-3 网络对抗技术 20165235 Exp3 免杀原理与实践
    2018-2019-2 20165235《网络对抗技术》Exp2 后门原理与实践
    Exp1 PC平台逆向破解 20165235 祁瑛
    2018-2019 20165235 网络对抗技术 Exp0:kali的安装
    20165302 Exp9 Web安全基础
    20165302 Exp 8 Web基础
    2018-2019-2 20165302 Exp7 网络欺诈防范
  • 原文地址:https://www.cnblogs.com/wucg/p/1742138.html
Copyright © 2011-2022 走看看