Skip to main content
gamedev.net
🔒 Locked

C# - Singleton base class

Started by ShockBoy Dec 25, 2002 at 11:08 PM 7 replies 4.6k views
Original Post
ShockBoy
ShockBoy
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!
--Before I couldn't spell engineer, now I are one!
JD
JD
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.
joanusdmentia
joanusdmentia
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.

"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o
the Speed Bump
the Speed Bump

  
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".
"There is only one everything"
ShockBoy
ShockBoy
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]
--Before I couldn't spell engineer, now I are one!
joanusdmentia
joanusdmentia
yeap, In VS.Net 2003 C# will be staring a feature called generics, which from what i''ve read is much like templates in C++.


Joanus D''Mentia
---------------
The three phases of me...
The twit, the tool and the lonely fool.
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o
Arild Fines
Arild Fines
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
--AnkhSVN - A Visual Studio .NET Addin for the Subversion version control system.[Project site] [IRC channel] [Blog]
joanusdmentia
joanusdmentia
really!? DAMN YOU BILL GATES!!!!!!

I''d heard that Bill had been talking about generics and assumed they''d be for 2003, oh well.


Joanus D''Mentia
---------------
The three phases of me...
The twit, the tool and the lonely fool
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The nly verdict is vengeance; a vendett o

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.