42 Spikes

Mike's ramblings on technology, agility, and the meaning of life

Categories: .Net, Patterns Posted by mheydt on 9/20/2006 1:32 AM | Comments (0)
I've been coding a bunch of singletons lately.  Don't know, but maybe it's just a fad but a lot of classes only need to allow a singleobject.  The pattern I usually use is the following:

public class Foo
{
    private static Foo instance = new Foo();
    public static Foo Instance
    {
       get { return instance; }
    }

    protected Foo() {}
}

Acces to the single object is then through the property as:

Foo.Instance.<methodname>

I'mjust thinking that this is way too much typing.  A wrist friendlylanguage (to quote the creator of 'boo').  I'd like to see thefollowing type of syntax for declaring a singleton class:

[Singleton]
public class Foo {}

The compiler / runtime should use the attribute to autogenerate the protected constructor, private member, and public property.