[Mono-list] Extending the singleton pattern in C#

Gaurav Vaish gaurav.vaish@amsoft.net
Wed, 11 Sep 2002 16:06:06 +0530


> sealed class Singleton
> {
> public static readonly Instance = new Singleton ();
> private Singleton () {...}
> }
> class Test {Singleton obj = Singleton.Instance;}
>
> However, this technique does not seem to be so extensible, since there's

    What do you mean by "extensible"? It's not quite clear here.

> no way to overload the private constructor (well, yes, but useless).

    Why useless?
    It's simpler than using attributes:

class Singleton
{
    public static type1 prop1;
    public static type2 prop2;

    private static Singleton instance = null;

    private Singleton()
    {
        // do something with prop1 and prop2.
    }

    public static Singleton Instance
    {
        get
        {
            if(instance == null)
            {
                instance = new Singleton();
            }
            return instance;
        }
    }
}


    I think this should satisfy you. Or are you looking for something more?



Happy hacking,
Gaurav
http://mastergaurav.virtualave.net/iitk
----------------------------

> One solution could be provided using attributes, e.g.
>
> class FooAttribute : Attribute
> {
> // whatever info I need
> }
> and later on,
> class Test
> {
> [Foo (...)]
> Singleton obj;
>
> public Test ()
> {
> obj = Singleton.Instance;
> }
> }
>
> My question is: how can I access (or transfer) the Foo declared data in my
Singleton
> instance constructor? If that's not possible, do you have any other
suggestions
> on extending my singleton? There might be other solutions, but I don't
catch'em.
>
> Best,
>
> Alexandru Pruteanu
> Faculty of Computer Science
> Iasi, Romania
> http://students.infoiasi.ro/~orion
>
>
> _______________________________________________
> Mono-list maillist  -  Mono-list@ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list