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
Post a Comment