ʼþ×ÜÏߣ¨Event Bus£©Öª¶àÉÙ
Ô´Âë·¾¶£ºGithub-EventBus
¼òÊéͬ²½Á´½Ó
ʼþ×ÜÏßÕâ¸ö¸ÅÄî¶ÔÄãÀ´Ëµ¿ÉÄܺÜÄ°Éú£¬µ«Ìáµ½¹Û²ìÕߣ¨·¢²¼-¶©ÔÄ£©Ä£Ê½£¬ÄãÒ²Ðí¾ÍºÜÊìϤ¡£Ê¼þ×ÜÏßÊǶԷ¢²¼-¶©ÔÄģʽµÄÒ»ÖÖʵÏÖ¡£ËüÊÇÒ»ÖÖ¼¯ÖÐʽʼþ´¦Àí»úÖÆ£¬ÔÊÐí²»Í¬µÄ×é¼þÖ®¼ä½øÐб˴ËͨÐŶøÓÖ²»ÐèÒªÏ໥ÒÀÀµ£¬´ïµ½Ò»ÖÖ½âñîµÄÄ¿µÄ¡£
ÎÒÃÇÀ´¿´¿´Ê¼þ×ÜÏߵĴ¦ÀíÁ÷³Ì£º
Á˽âÁËʼþ×ÜÏߵĻù±¾¸ÅÄîºÍ´¦ÀíÁ÷³Ì£¬ÏÂÃæÎÒÃǾÍÀ´·ÖÎöÏÂÈçºÎȥʵÏÖʼþ×ÜÏß¡£
2.»Ø¹é±¾ÖÊÔÚ¶¯ÊÖʵÏÖʼþ×ÜÏß֮ǰ£¬ÎÒÃÇ»¹ÊÇҪ׷±¾ËÝÔ´£¬Ì½Ë÷Ò»ÏÂʼþµÄ±¾Öʺͷ¢²¼¶©ÔÄģʽµÄʵÏÖ»úÖÆ¡£
2.1.ʼþµÄ±¾ÖÊÎÒÃÇÏÈÀ´Ì½ÌÖÒ»ÏÂʼþµÄ¸ÅÄî¡£¶¼ÊǶÁ¹ýÊéµÄ£¬Ó¦¸Ã¶¼»¹¼ÇµÃ¼ÇÐðÎĵÄÁùÒªËØ£ºÊ±¼ä¡¢µØµã¡¢ÈËÎʼþ£¨ÆðÒò¡¢¾¹ý¡¢½á¹û£©¡£
ÎÒÃÇÄÃ×¢²áµÄ°¸Àý£¬À´½âÊÍһϡ£
Óû§ÊäÈëÓû§Ãû¡¢ÓÊÏä¡¢ÃÜÂëºó£¬µã»÷×¢²á£¬ÊäÈëÎÞÎóУÑéͨ¹ýºó£¬×¢²á³É¹¦²¢·¢ËÍÓʼþ¸øÓû§£¬ÒªÇóÓû§½øÐÐÓÊÏäÑéÖ¤¼¤»î¡£
ÕâÀïÃæ¾ÍÉæ¼°ÁËÁ½¸öÖ÷Ҫʼþ£º
ÆäʵÕâÁùÒªËØÒ²ÊÊÓÃÓÚÎÒÃdzÌÐòÖÐʼþµÄ´¦Àí¹ý³Ì¡£¿ª·¢¹ýWinForm³ÌÐòµÄ¶¼ÖªµÀ£¬ÎÒÃÇÔÚ×öUIÉè¼ÆµÄʱºò£¬´Ó¹¤¾ßÏäÍÏÈëÒ»¸ö×¢²á°´Å¥£¨btnRegister£©£¬Ë«»÷Ëü£¬VS¾Í»á×Ô¶¯°ïÎÒÃÇÉú³ÉÈçÏ´úÂ룺
void btnRegister_Click(object sender, EventArgs e) { // ʼþµÄ´¦Àí }ÆäÖÐobject senderÖ¸´ú·¢³öʼþµÄ¶ÔÏó£¬ÕâÀïÒ²¾ÍÊÇbutton¶ÔÏó£»EventArgs e ʼþ²ÎÊý£¬¿ÉÒÔÀí½âΪ¶ÔʼþµÄÃèÊö £¬ËüÃÇ¿ÉÒÔͳ³ÆΪʼþÔ´¡£ÆäÖеĴúÂëÂß¼£¬¾ÍÊǶÔʼþµÄ´¦Àí¡£ÎÒÃÇ¿ÉÒÔͳ³ÆΪʼþ´¦Àí¡£
˵ÁËÕâô¶à£¬ÎÞ·ÇÊÇÏë͸¹ýÏÖÏó¿´±¾ÖÊ£ºÊ¼þÊÇÓÉʼþÔ´ºÍʼþ´¦Àí×é³É¡£
2.2. ·¢²¼¶©ÔÄģʽ¶¨Òå¶ÔÏó¼äÒ»ÖÖÒ»¶Ô¶àµÄÒÀÀµ¹Øϵ£¬Ê¹µÃÿµ±Ò»¸ö¶ÔÏó¸Ä±ä״̬£¬ÔòËùÓÐÒÀÀµÓÚËüµÄ¶ÔÏ󶼻áµÃµ½Í¨Öª²¢±»×Ô¶¯¸üС£ ¡ª¡ª·¢²¼¶©ÔÄģʽ
·¢²¼¶©ÔÄģʽÖ÷ÒªÓÐÁ½¸ö½ÇÉ«£º
·¢²¼¶©ÔÄģʽÓÐÁ½ÖÖʵÏÖ·½Ê½£º
×ܵÄÀ´Ëµ£¬·¢²¼¶©ÔÄģʽÖÐÓÐÁ½¸ö¹Ø¼ü×Ö£¬Í¨ÖªºÍ¸üС£
±»¹Û²ìÕß״̬¸Ä±ä֪ͨ¹Û²ìÕß×ö³öÏàÓ¦¸üС£
½â¾öµÄÊǵ±¶ÔÏó¸Ä±äʱÐèҪ֪ͨÆäËû¶ÔÏó×ö³öÏàÓ¦¸Ä±äµÄÎÊÌâ¡£
Èç¹û»Ò»¸öͼÀ´±íʾÕâ¸öÁ÷³ÌµÄ»£¬Í¼ÐÎÓ¦¸ÃÊÇÕâÑùµÄ£º
3 ʵÏÖ·¢²¼¶©ÔÄģʽÏàÐÅͨ¹ýÉÏÃæµÄ½âÊÍ£¬¶ÔʼþºÍ·¢²¼¶©ÔÄģʽÓÐÁËÒ»¸ö´ó¸ÅµÄÓ¡Ï󡣶¼ËµÀíÂÛÒªÓëʵ¼ùÏà½áºÏ£¬ËùÒÔÎÒÃÇ»¹ÊǶ¯¶¯ÊÖÖ¸ÇÃÇôúÂë±È½ÏºÃ¡£
ÎÒ½«ÒÔ¡º¹Û²ìÕßģʽ¡»À´µöÓãÕâ¸öÀý×ÓΪ»ù´¡£¬Í¨¹ýÖع¹µÄ·½Ê½À´ÍêÉÆÒ»¸ö¸ü¼ÓͨÓõķ¢²¼¶©ÔÄģʽ¡£
ÏÈÉÏ´úÂ룺
µöÓã¸ÍµÄʵÏÖ£º
/// <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ÉÏÃæµÄίÍÐʵÀý¡£
ÎÊÌâÕÒµ½ÁË£¬ºÜÏÔÈ»ÊÇÎÒÃǵÄʼþÔ´ºÍʼþ´¦Àí²»¹»³éÏó£¬ËùÒÔ²»ÄÜͨÓã¬ÏÂÃæÔÛÃǾÍÀ´¶¯ÊÖ¸ÄÔì¡£
¡¡