C # singleton pattern in static class?

My event manager class freely connects instances of other classes, implementing a signal-slot pattern pattern.

The application assumes only one unique event dispatcher. Since my class Dispatcher, which does all the work, inherits from Dictionary<TKey, TValue>, it cannot be declared static.

To overcome this limitation, I applied a basic static wrapper class EVDwith a private property EVDthat provides a singleton Dispatcher, as well as public static methods Subscribe, UnSubscribeand Dispatchthat wrap singleton relevant methods:

namespace EventDispatcher
{
    public static class EVD
    {
        static Dispatcher evd { get { return Singleton<Dispatcher>.Instance; } }

        public static void Subscribe(string name, EvtHandler handler)
        {
            evd.Subscribe(name, handler);
        }
        public static void UnSubscribe(string name, EvtHandler handler = null)
        {
            evd.UnSubscribe(name, handler);
        }
        public static void Dispatch(string name, object sender, EvtArgs e = null)
        {
            evd.Dispatch(name, sender, e);
        }
    }

    class Dispatcher : Dictionary<string, Event> { /* main event dispatcher */ }

    static class Singleton<T> where T : /* generic singleton creation */
}

So here is my question:

singleton ? AFAIK . , , EVD :

static Dispatcher evd = new Dispatcher();

? , Lazy<T> .

:

static Dispatcher _evd;
static Dispatcher evd 
{ 
    get { return _evd ?? (_evd = new Dispatcher()); }
}

, ...

,

+4

All Articles