[Mono-list] Extending the singleton pattern in C#
PRUTEANU Teodor Alexandru
orion@infoiasi.ro
Wed, 11 Sep 2002 14:13:24 +0300 (EEST)
Hello Gaurav,
1.. Reasons for using this implementation for the singleton pattern:
there's an article on msdn.microsoft.com, "Exploring the Sigleton
Design Pattern", written by Mark Townsend, Feb 2002. The solution takes
advantage from the CLI desing: removes lazy-inits, double locks (and so
on) and it is *thread-safe*.
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 :-)
3.. Your solution does not satisfy me. Yes, I'm looking for more (2).
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