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

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


> Hello Gaurav,

Hi there,

    Let me see if I am able to do a little more for you.

> 2.. I meant !extensible in the way that the current solution only provides
> one default ctor, so one user cannot instantiate the Singleton instance
> with his/her defined data.
> I'm looking for smth like:
> public static readonly Instance = new Singleton
> (user-defined data extracted from custom attributes).
> Hope you understand now my "extensibility" problem :-)

public class Singleton
{
    private static Singleton instance = null;

    private property type1 prop1;
    private property type2 prop2;

    private Singleton()
    {
        // do something. Throw an exception!
    }

    private Singleton(type1 one, type2 two)
    {
        this.prop1 = one;
        this.prop1 = two;
        // do something with prop1 and prop2
        // throw an exception if necessary.
    }

    public static Singleton GetSingleton(type1 one, type2 two)
    {
        if(instance == null)
            instance = new Singleton(one, two);
        return instance;
    }
}

    The parameters that you were passing to "Attribute" can now be passed to
the method.
    Well, I don't think you can demand more than this, or can you? ;-)

> 3.. Your solution does not satisfy me. Yes, I'm looking for more (2).

    Let me know if you are still hungry. :-)


Happy hacking,
Gaurav
http://mastergaurav.virtualave.net/iitk
----------------------------
>
> Best.
>
> Alexandru Pruteanu
> Faculty of Computer Science
> Iasi, Romania
> http://students.infoiasi.ro/~orion
>
>
> > 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
>
>
> http://students.infoiasi.ro/~orion
>
>