Once the singleton pattern is understood, writing subsequent singleton classes in C# is a brainless exercise. I would hope that the framework would help you by providing an interface or a base class to do that. Here is how I envision it:
public sealed class Schablone : ISingleton<Schablone>
{
// Stuff forced by the interface goes here
// Extra logic goes here
}
Does what I am looking for exist?
Is there some syntactic sugar for constructing a singleton class – whether with an interface, a class attribute, etc.?
Can one write a useful and bullet-proof ISingleton themselves? Care to try?
Thanks!
No, there is nothing in the .Net framework like this. You have to write singletons yourself. It would be a bit hard to have a definative API for singletons as there are so many ways of implementing them, and depending on your goals depends on what approach you will take.
I would suggest you read this article on singletons. (Written by Jon – He’s a wise guy, listen to him) for guidance and tips on the various techniques. It covers some stuff that most people might not have thought about.
[A lot of people consider the singleton pattern to be a dubious design pattern. I won’t go into the reasons here, but if your interested google has loads of info. Just be careful you don’t overuse it unnecessarily.]