El TecnoBaúl de Kiquenet

Kiquenet boring stories

Posts Tagged ‘patrones’

Singleton genérico

Posted by kiquenet en 9 julio 2010

public abstract class Singleton<T> where T : class
    {
        private static T _instance = (T)Activator.CreateInstance(typeof(T), true);

        public static T Instance
        {
            get { return _instance; }
        }
    }

La implementación:

public class FooBarSvc : Singleton<FooBarSvc>

{

private FooBarSvc() { }

public void Foo(){ ...}

}

Uso: FooBarSvc.Instance.Foo();

http://scottwhite.blogspot.com/2010/03/when-you-implement-non-threadsafe.html

Anuncios

Posted in .NET | Etiquetado: , | Leave a Comment »