Can someone please tell me the difference between these two different approaches. I know that both of them can handle the same scenario but is there any pros/cons of using the first or the second approach? What would you guys go for and why?
Direct subscription to the 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
{
get
{
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;