Archived

This topic is now archived and is closed to further replies.

ShockBoy

C# - Singleton base class

Recommended Posts

I already understand all the pros and cons to singleton objects, so don''t bother posting about those. Because C# doesn''t have templates, I''m not sure how to create a singleton base class that other classes could derive from. For example in C++, i would do this: class MyClass : public CSingleton {...}; This would be impossible in C#. So, is there any way else I could do this? I don''t want to reimplement the Instance() method in all derived classes. Thanks Before I couldn''t spell engineer, now I are one!

Share this post


Link to post
Share on other sites
Singletons don''t need templates. It''s just a static member of a class like so:


class Singleton
{
static Singleton s;
Singleton(int _k):k(_k){}
Singleton(const Singleton &);// prevent copy construction
int k;
public:
static Singleton* Instance(){return &s;}
};

Singleton Singleton::s(12);


I don''t know how c# handles the static object creation across files.

Share this post


Link to post
Share on other sites
have your inhereted class pass back the instance for the singleton to use through a private abstract function and have your CSingleton class do it''s thing using an object reference (as in the ''object'' base class of everything). For when you want a reference to that instance the inhereted class could also override a public abstract to do the cast from object to whatever.


  
class Singleton
{
...
void Init()
{
objectref = InitReference();
}

protected:
...
abstract object GetReference();

private:
...
abstract object InitReference();

object objectref;
}

class Schtuph : Singleton
{
...
Schtuph Get()
{
return (Schtuph)GetReference();
}

private:
...
object InitReference()
{
static Schtuph objectref = new Schtuph();
return objectref;
}
}


now i''m not an expert on singletons so I don''t know if it''s more involved that storing a static instance of the class within the class itself, but if it isn''t then all of this is really pointless and would be just as easy to implement in each class seperately. If there is more to it though, then I think this should work.

Share this post


Link to post
Share on other sites

  
class MySingleton // no point in making a generic base class

{
private MySingleton() { ... }
public static readonly MySingleton instance = new MySingleton(); // no point in encapsulating and making an accessor since it''s readonly (no harm in doing so, though)


// other methods here

}


I''m hip because I say "M$" instead of "MS".

Share this post


Link to post
Share on other sites
quote:
Original post by ShockBoy

class MyClass : public CSingleton
{...};




What I really meant was this:


    
class MyClass : public CSingleton <MyClass>
{...};


I can't put in the inequality signs since it thinks they are html tags. Hopefully this worked instead.

I agree with all of you that you can just place an instance object in each class, and it may be easy to do so. But my problem is that C++ was able to encapsulate (maybe wrong word) singletons into a class of its own. By making that class a base class, you could make any class into a singleton automagically. It's too bad C# doesn't have the template capabilities, but I hear rumors that it may come in the next release (but I'm not holding my breath)

As for now, I'll probably just do the static instance in each class, maybe make an interface so that each class must obey my singleton standard.

But first of all, I'm going to get a book on C#, starting to get lost in the inheritance and polymorphism stuff. A little different than C++.

Thanks for all the help guys.


Before I couldn't spell engineer, now I are one!

[edited by - ShockBoy on December 26, 2002 2:07:38 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by joanusdmentia
yeap, In VS.Net 2003 C# will be staring a feature called generics,

Nope. No generics until VS.NET For Yukon, probably some time in 2004.



For those who believe in God, most of the big questions are answered. But for those of us who can''t readily accept the God formula, the big answers don''t remain stone- written. We adjust to new conditions and discoveries. We are pliable. Love need not be a command or faith a dictum. I am my own God. We are here to unlearn the teachings of the church, state, and our educational system. We are here to drink beer. We are here to kill war. We are here to laugh at the odds and live our lives so well that Death will tremble to take us -- Charles Bukowski

Share this post


Link to post
Share on other sites