c# - Event-Subscriptions using static events or subscription via a Singleton? -


this question has answer here:

can please tell me difference between these 2 different approaches. know both of them can handle same scenario there pros/cons of using first or second approach? guys go , why?

direct subscription event (using static event) :

    public class myeffect : baseeffect     {       public delegate void eventhandler (element sender, unit target);       public static event eventhandler onmyevent;        public damage(element elementowner)       {         this.owner = elementowner;       }        public void dispatchevent(unit target)       {         if (onmyevent != null)         {             onmyevent(this.owner, target);         }       }    } 

=> event subscription :

      myeffect.onmyevent += callback; 

or subscription via singleton :

    public class myeffect : baseeffect     {       public delegate void eventhandler (element sender, unit target);       public event eventhandler onmyevent;        private static myeffect instance;        public static myeffect instance       {                 {             return instance;         }       }        public damage(element elementowner)       {         this.owner = elementowner;       }        public void dispatchevent(unit target)       {         if (onmyevent != null)         {             onmyevent(this.owner, target);         }       }    } 

=> event subscription :

      myeffect.instance.onmyevent += callback; 

use first approach. object have instantiate light-weight , unless have obvious reason use singleton (expensive object or heavily used et al.), adopt simplest approach; first of above.


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -