HTML5¼¼Êõ

ʼþ×ÜÏߣ¨Event Bus£©Öª¶àÉÙ - ¡ºÊ¥½Ü¡»

×ÖºÅ+ ×÷ÕߣºH5Ö®¼Ò À´Ô´£ºH5Ö®¼Ò 2017-06-09 14:01 ÎÒÒªÆÀÂÛ( )

ʼþ×ÜÏߣ¨Event Bus£©Öª¶àÉÙ Ô´Âë·¾¶£ºGithub-EventBus ¼òÊéͬ²½Á´½Ó 1. ÒýÑÔ Ê¼þ×ÜÏßÕâ¸ö¸ÅÄî¶ÔÄãÀ´Ëµ¿ÉÄܺÜÄ°Éú£¬µ«Ìáµ½¹Û²ìÕߣ¨·¢²¼-¶©ÔÄ£©Ä£Ê½£¬ÄãÒ²Ðí¾ÍºÜÊìϤ¡£Ê¼þ×ÜÏßÊǶԷ¢²¼-¶©ÔÄģʽµÄÒ»ÖÖʵÏÖ¡£ËüÊÇÒ»ÖÖ¼¯ÖÐʽʼþ´¦Àí»úÖÆ£¬ÔÊÐí²»Í¬µÄ×é¼þÖ®

ʼþ×ÜÏߣ¨Event Bus£©Öª¶àÉÙ

Ô´Âë·¾¶£ºGithub-EventBus
¼òÊéͬ²½Á´½Ó

1. ÒýÑÔ

ʼþ×ÜÏßÕâ¸ö¸ÅÄî¶ÔÄãÀ´Ëµ¿ÉÄܺÜÄ°Éú£¬µ«Ìáµ½¹Û²ìÕߣ¨·¢²¼-¶©ÔÄ£©Ä£Ê½£¬ÄãÒ²Ðí¾ÍºÜÊìϤ¡£Ê¼þ×ÜÏßÊǶԷ¢²¼-¶©ÔÄģʽµÄÒ»ÖÖʵÏÖ¡£ËüÊÇÒ»ÖÖ¼¯ÖÐʽʼþ´¦Àí»úÖÆ£¬ÔÊÐí²»Í¬µÄ×é¼þÖ®¼ä½øÐб˴ËͨÐŶøÓÖ²»ÐèÒªÏ໥ÒÀÀµ£¬´ïµ½Ò»ÖÖ½âñîµÄÄ¿µÄ¡£

ÎÒÃÇÀ´¿´¿´Ê¼þ×ÜÏߵĴ¦ÀíÁ÷³Ì£º

ͼ1£ºEventBusÁ÷³Ì

Á˽âÁËʼþ×ÜÏߵĻù±¾¸ÅÄîºÍ´¦ÀíÁ÷³Ì£¬ÏÂÃæÎÒÃǾÍÀ´·ÖÎöÏÂÈçºÎȥʵÏÖʼþ×ÜÏß¡£

2.»Ø¹é±¾ÖÊ

ÔÚ¶¯ÊÖʵÏÖʼþ×ÜÏß֮ǰ£¬ÎÒÃÇ»¹ÊÇҪ׷±¾ËÝÔ´£¬Ì½Ë÷Ò»ÏÂʼþµÄ±¾Öʺͷ¢²¼¶©ÔÄģʽµÄʵÏÖ»úÖÆ¡£

2.1.ʼþµÄ±¾ÖÊ

ÎÒÃÇÏÈÀ´Ì½ÌÖÒ»ÏÂʼþµÄ¸ÅÄî¡£¶¼ÊǶÁ¹ýÊéµÄ£¬Ó¦¸Ã¶¼»¹¼ÇµÃ¼ÇÐðÎĵÄÁùÒªËØ£ºÊ±¼ä¡¢µØµã¡¢ÈËÎʼþ£¨ÆðÒò¡¢¾­¹ý¡¢½á¹û£©¡£

ÎÒÃÇÄÃ×¢²áµÄ°¸Àý£¬À´½âÊÍһϡ£
Óû§ÊäÈëÓû§Ãû¡¢ÓÊÏä¡¢ÃÜÂëºó£¬µã»÷×¢²á£¬ÊäÈëÎÞÎóУÑéͨ¹ýºó£¬×¢²á³É¹¦²¢·¢ËÍÓʼþ¸øÓû§£¬ÒªÇóÓû§½øÐÐÓÊÏäÑéÖ¤¼¤»î¡£

ÕâÀïÃæ¾ÍÉæ¼°ÁËÁ½¸öÖ÷Ҫʼþ£º

  • ×¢²áʼþ£ºÆðÒòÊÇÓû§µã»÷ÁË×¢²á°´Å¥£¬¾­¹ýÊÇÊäÈëУÑ飬½á¹ûÊÇÊÇ·ñ×¢²á³É¹¦¡£
  • ·¢ËÍÓʼþʼþ£ºÆðÒòÊÇÓû§Ê¹ÓÃÓÊÏä×¢²á³É¹¦ÐèÒªÑéÖ¤ÓÊÏ䣬¾­¹ýÊÇÓʼþ·¢ËÍ£¬½á¹ûÊÇÓʼþÊÇ·ñ·¢Ëͳɹ¦¡£
  • ÆäʵÕâÁùÒªËØÒ²ÊÊÓÃÓÚÎÒÃdzÌÐòÖÐʼþµÄ´¦Àí¹ý³Ì¡£¿ª·¢¹ýWinForm³ÌÐòµÄ¶¼ÖªµÀ£¬ÎÒÃÇÔÚ×öUIÉè¼ÆµÄʱºò£¬´Ó¹¤¾ßÏäÍÏÈëÒ»¸ö×¢²á°´Å¥£¨btnRegister£©£¬Ë«»÷Ëü£¬VS¾Í»á×Ô¶¯°ïÎÒÃÇÉú³ÉÈçÏ´úÂ룺

    void btnRegister_Click(object sender, EventArgs e) { // ʼþµÄ´¦Àí }

    ÆäÖÐobject senderÖ¸´ú·¢³öʼþµÄ¶ÔÏó£¬ÕâÀïÒ²¾ÍÊÇbutton¶ÔÏó£»EventArgs e ʼþ²ÎÊý£¬¿ÉÒÔÀí½âΪ¶ÔʼþµÄÃèÊö £¬ËüÃÇ¿ÉÒÔͳ³ÆΪʼþÔ´¡£ÆäÖеĴúÂëÂß¼­£¬¾ÍÊǶÔʼþµÄ´¦Àí¡£ÎÒÃÇ¿ÉÒÔͳ³ÆΪʼþ´¦Àí¡£

    ˵ÁËÕâô¶à£¬ÎÞ·ÇÊÇÏë͸¹ýÏÖÏó¿´±¾ÖÊ£ºÊ¼þÊÇÓÉʼþÔ´ºÍʼþ´¦Àí×é³É¡£

    2.2. ·¢²¼¶©ÔÄģʽ

    ¶¨Òå¶ÔÏó¼äÒ»ÖÖÒ»¶Ô¶àµÄÒÀÀµ¹Øϵ£¬Ê¹µÃÿµ±Ò»¸ö¶ÔÏó¸Ä±ä״̬£¬ÔòËùÓÐÒÀÀµÓÚËüµÄ¶ÔÏ󶼻áµÃµ½Í¨Öª²¢±»×Ô¶¯¸üС£ ¡ª¡ª·¢²¼¶©ÔÄģʽ

    ·¢²¼¶©ÔÄģʽÖ÷ÒªÓÐÁ½¸ö½ÇÉ«£º

  • ·¢²¼·½£¨Publisher£©£ºÒ²³ÆΪ±»¹Û²ìÕߣ¬µ±×´Ì¬¸Ä±äʱ¸ºÔð֪ͨËùÓж©ÔÄÕß¡£
  • ¶©ÔÄ·½£¨Subscriber£©£ºÒ²³ÆΪ¹Û²ìÕߣ¬¶©ÔÄʼþ²¢¶Ô½ÓÊÕµ½µÄʼþ½øÐд¦Àí¡£
  • ·¢²¼¶©ÔÄģʽÓÐÁ½ÖÖʵÏÖ·½Ê½£º

  • ¼òµ¥µÄʵÏÖ·½Ê½£ºÓÉPublisherά»¤Ò»¸ö¶©ÔÄÕßÁÐ±í£¬µ±×´Ì¬¸Ä±äʱѭ»·±éÀúÁбí֪ͨ¶©ÔÄÕß¡£
  • ίÍеÄʵÏÖ·½Ê½£ºÓÉPublisher¶¨ÒåʼþίÍУ¬SubscriberʵÏÖίÍС£
  • ×ܵÄÀ´Ëµ£¬·¢²¼¶©ÔÄģʽÖÐÓÐÁ½¸ö¹Ø¼ü×Ö£¬Í¨ÖªºÍ¸üС£
    ±»¹Û²ìÕß״̬¸Ä±ä֪ͨ¹Û²ìÕß×ö³öÏàÓ¦¸üС£
    ½â¾öµÄÊǵ±¶ÔÏó¸Ä±äʱÐèҪ֪ͨÆäËû¶ÔÏó×ö³öÏàÓ¦¸Ä±äµÄÎÊÌâ¡£

    Èç¹û»­Ò»¸öͼÀ´±íʾÕâ¸öÁ÷³ÌµÄ»­£¬Í¼ÐÎÓ¦¸ÃÊÇÕâÑùµÄ£º

    ͼ2£º·¢²¼¶©ÔÄģʽÁ÷³Ì

    3 ʵÏÖ·¢²¼¶©ÔÄģʽ

    ÏàÐÅͨ¹ýÉÏÃæµÄ½âÊÍ£¬¶ÔʼþºÍ·¢²¼¶©ÔÄģʽÓÐÁËÒ»¸ö´ó¸ÅµÄÓ¡Ï󡣶¼ËµÀíÂÛÒªÓëʵ¼ùÏà½áºÏ£¬ËùÒÔÎÒÃÇ»¹ÊǶ¯¶¯ÊÖÖ¸ÇÃÇôúÂë±È½ÏºÃ¡£
    ÎÒ½«ÒÔ¡º¹Û²ìÕßģʽ¡»À´µöÓãÕâ¸öÀý×ÓΪ»ù´¡£¬Í¨¹ýÖع¹µÄ·½Ê½À´ÍêÉÆÒ»¸ö¸ü¼ÓͨÓõķ¢²¼¶©ÔÄģʽ¡£
    ÏÈÉÏ´úÂ룺

    /// <summary> /// ÓãµÄÆ·Ààö¾Ù /// </summary> public enum FishType { öêÓã, ÀðÓã, ºÚÓã, ÇàÓã, ²ÝÓã, öÔÓã }

    µöÓã¸ÍµÄʵÏÖ£º

    /// <summary> /// Óã¸Í£¨±»¹Û²ìÕߣ© /// </summary> public class FishingRod { public delegate void FishingHandler(FishType type); //ÉùÃ÷ίÍÐ public event FishingHandler FishingEvent; //ÉùÃ÷ʼþ public void ThrowHook(FishingMan man) { Console.WriteLine("¿ªÊ¼Ï¹³£¡"); //ÓÃËæ»úÊýÄ£ÄâÓãÒ§¹³£¬ÈôËæ»úÊýΪżÊý£¬ÔòΪÓãÒ§¹³ if (new Random().Next() % 2 == 0) { var type = (FishType) new Random().Next(0, 5); Console.WriteLine("Áåîõ£º¶£¶£¶££¬Óã¶ùÒ§¹³ÁË"); if (FishingEvent != null) FishingEvent(type); } } }

    ´¹µöÕߣº

    /// <summary> /// ´¹µöÕߣ¨¹Û²ìÕߣ© /// </summary> public class FishingMan { public FishingMan(string name) { Name = name; } public string Name { get; set; } public int FishCount { get; set; } /// <summary> /// ´¹µöÕß×ÔȻҪÓÐÓã¸Í°¡ /// </summary> public FishingRod FishingRod { get; set; } public void Fishing() { this.FishingRod.ThrowHook(this); } public void Update(FishType type) { FishCount++; Console.WriteLine("{0}£ºµöµ½Ò»Ìõ[{2}]£¬ÒѾ­µöµ½{1}ÌõÓãÁË£¡", Name, FishCount, type); } }

    ³¡¾°ÀàÒ²ºÜ¼òµ¥£º

    //1¡¢³õʼ»¯Óã¸Í var fishingRod = new FishingRod(); //2¡¢ÉùÃ÷´¹µöÕß var jeff = new FishingMan("Ê¥½Ü"); //3.·ÖÅäÓã¸Í jeff.FishingRod = fishingRod; //4¡¢×¢²á¹Û²ìÕß fishingRod.FishingEvent += jeff.HandleEvent; //5¡¢Ñ­»·µöÓã while (jeff.FishCount < 5) { jeff.Fishing(); Console.WriteLine("-------------------"); //˯Ãß5s Thread.Sleep(5000); }

    ´úÂëºÜ¼òµ¥£¬ÏàÐÅÄãÒ»¿´¾ÍÃ÷°×¡£µ«ºÜÏÔÈ»Õâ¸ö´úÂëʵÏÖ½öÊÊÓÃÓÚµ±Ç°Õâ¸öµöÓ㳡¾°£¬¼ÙÈçÓÐÆäËû³¡¾°Ò²ÏëʹÓÃÕâ¸öģʽ£¬ÎÒÃÇ»¹ÐèÒªÖØж¨ÒåίÍУ¬ÖØж¨Òåʼþ´¦Àí£¬Æñ²»ºÜÀÛ¡£±¾×Å¡±Don't repeat yourself¡°µÄÔ­Ôò£¬ÎÒÃÇÒª¶ÔÆä½øÐÐÖع¹¡£

    ½áºÏÎÒÃǶÔʼþ±¾ÖʵÄ̽ÌÖ£¬Ê¼þÊÇÓÉʼþÔ´ºÍʼþ´¦Àí×é³É¡£Õë¶ÔÎÒÃÇÉÏÃæµÄ°¸ÀýÀ´Ëµ£¬public delegate void FishingHandler(FishType type);Õâ¾ä´úÂë¾ÍÒѾ­ËµÃ÷ÁËʼþÔ´ºÍʼþ´¦Àí¡£Ê¼þÔ´¾ÍÊÇFishType type£¬Ê¼þ´¦Àí×ÔÈ»ÊÇ×¢²áµ½FishingHandlerÉÏÃæµÄίÍÐʵÀý¡£
    ÎÊÌâÕÒµ½ÁË£¬ºÜÏÔÈ»ÊÇÎÒÃǵÄʼþÔ´ºÍʼþ´¦Àí²»¹»³éÏó£¬ËùÒÔ²»ÄÜͨÓã¬ÏÂÃæÔÛÃǾÍÀ´¶¯ÊÖ¸ÄÔì¡£

    3.1. ÌáȡʼþÔ´

    ¡¡

    1.±¾Õ¾×ñÑ­ÐÐÒµ¹æ·¶£¬ÈκÎתÔصĸå¼þ¶¼»áÃ÷È·±ê×¢×÷ÕߺÍÀ´Ô´£»2.±¾Õ¾µÄÔ­´´ÎÄÕ£¬ÇëתÔØʱÎñ±Ø×¢Ã÷ÎÄÕÂ×÷ÕߺÍÀ´Ô´£¬²»×ðÖØÔ­´´µÄÐÐΪÎÒÃǽ«×·¾¿ÔðÈΣ»3.×÷ÕßͶ¸å¿ÉÄܻᾭÎÒÃDZ༭Ð޸Ļò²¹³ä¡£

    Ïà¹ØÎÄÕÂ
    • [ÎÊÌâÌù]mui.openWindow+×Ô¶¨Òåʼþ¼àÌý²Ù×÷ÈÃalert()Ö´ÐÐÁ½´Î - CN_Simo

      [ÎÊÌâÌù]mui.openWindow+×Ô¶¨Òåʼþ¼àÌý²Ù×÷ÈÃalert()Ö´ÐÐÁ½´Î - CN_

      2017-05-27 10:02

    • HTML5Ö§³Ö·þÎñÆ÷·¢ËÍʼþ(Server-Sent Events)-µ¥ÏòÏûÏ¢´«µÝÊý¾ÝÍÆËÍ£¨C#ʾÀý£© - ÐÜ×ÐÆäÈË

      HTML5Ö§³Ö·þÎñÆ÷·¢ËÍʼþ(Server-Sent Events)-µ¥ÏòÏûÏ¢´«µÝÊý¾ÝÍÆËÍ

      2017-05-19 11:02

    • HTML5ÖеÄÐÂʼþ - zxyGo

      HTML5ÖеÄÐÂʼþ - zxyGo

      2017-03-10 14:00

    • Òƶ¯¶ËtapʼþµÄ·â×° - Ò»»ìÎåÁùÄê

      Òƶ¯¶ËtapʼþµÄ·â×° - Ò»»ìÎåÁùÄê

      2017-02-28 18:00

    ÍøÓѵãÆÀ
    Ê