singleton

What does your Objective-C singleton look like?

Mine is merely (or a close variant thereof): static MyClass *gInstance = NULL; + (MyClass *)instance { @synchronized(self) { if (gInstance == NULL) gInstance = [[self alloc] init]; } return(gInstance); } ...

Implementing cache correctly in a class library for use in an asp.net application

I'm implementing a cache in a class library that i'm using in an asp.net application. I created my cache object as a singleton pattern with a static method to update the cache which is really just loading a member variable/property with a collection of data i need cached (got some locking logic ofcourse). I figured it was a nice way to...

Are there any viable alternatives to the GOF Singleton Pattern?

Let's face it. The Singleton Pattern is highly controversial topic with hordes programmers on both sides of the fence. There are those who feel like the Singleton is nothing more then a glorified global variable, and others who swear by pattern and use it incessantly. I don't want the Singleton Controversy to lie at the heart of my qu...

How can I create a thread-safe singleton pattern in Windows?

I've been reading about thread-safe singleton patterns here: http://en.wikipedia.org/wiki/Singleton_pattern#C.2B.2B_.28using_pthreads.29 And it says at the bottom that the only safe way is to use pthread_once - which isn't available on Windows. Is that the only way of guaranteeing thread safe initialisation? I've read this thread on ...

which scope should a DAO typically have.

its out of question that a dao will not hold any state. however, for easiest access to the class, is it better to use prototype( = new every time) or singleton? simple object creation is cheap for dao's.. it typically only holds a sessionfactory, accessing the object from a list of singletons may be equally expensive. clarfication: t...

Creating the Singleton design pattern in PHP5

How would one create a Singleton class using PHP5 classes? ...

Invoking a method using reflection on a singleton object

So I have the following: public class Singleton { private Singleton(){} public static readonly Singleton instance = new Singleton(); public string DoSomething(){ ... } public string DoSomethingElse(){ ... } } Using reflection how can I invoke the DoSomething Method? Reason I ask is because I store the method names in XM...

What exactly is the singleton class in ruby?

It seems as if I'm missing the point or misunderstanding the significance of the singleton class in Ruby. I've heard and read it in many ways—some more complicated than others—and left more confused as to what it is. Is it a class in and of itself? Is it the reason why all objects belong to "class?" The concept in play here is fuzzy. I'm...

What's the difference in these ways of creating the static instance for a singleton?

I have had a bug recently that only manifested itself when the library was built as a release build rather than a debug build. The library is a .NET dll with a COM wrapper and I am using CoCreateInstance to create a class from the dll in an unmanaged c++ app. When I finally tracked the bug down it was caused by accessing a singleton ob...

Disposable singleton in C#

I have a singleton that uses the "static readonly T Instance = new T();" pattern. However, I ran into a case where T is disposable, and actually needs to be disposed for unit tests. How can I modify this pattern to support a disposable singleton? The interface I would like is something like: var x = Foo.Instance; var y = Foo.Instance; ...

Singleton in Java - I didn't do it

I'm trying to track down an issue in our system and the following code worries me. The following occurs in our doPost() method in the primary servlet (names have been changed to protect the guilty): ... if(Single.getInstance().firstTime()){ doPreperations(); } normalResponse(); ... The singleton 'Single' looks like this: private ...

On Design Patterns: When to use the Singleton?

The glorified global variable - becomes a gloried global class. Some say breaking Object Oriented Design. Give me scenarios, other than the good old logger where it makes sense to use the singleton. ...

Is there a name meaning "not a singleton"?

Is there a name meaning "not a singleton"? ...

How to implement a singleton in C#?

How do I implement the singleton pattern in C#? I want to put my constants and some basic functions in it as I use those everywhere in my project. I want to have them 'Global' and not need to manually bind them every object I create. ...

Practical Singleton & Dependency Injection question

Say I have a class called PermissionManager which should only exist once for my system and basically fulfills the function of managing various permissions for various actions in my application. Now I have some class in my application which needs to be able to check a certain permission in one of its methods. This class's constructor is c...

What is a mono singleton?

A friend of mine was recently asked in a job interview to tell the difference between a standard singleton and a mono singleton. I have never heard the term before and a simple google search does not return any meaningful results. My friend suggested that it is an object where the constructor is public but all the members are static. T...

Singleton Data Access Layers

In our data access layer at work we have this standard implementation where the class is accessed through a singleton public property which looks something like this: public static CustomerController Instance { get { lock(singletonLock) { if( _instance == null ) { _instance = new ...

COM+ Singleton Numerous References

I have a COM+ data service that is configured to use object pooling with a min and max pool size of 1. So I have a singleton. In some scenarios my Object count (the number of clients that have a reference to this instance) goes beyond 1 and steadily increases. The instance creation and the one call to its method is wrapped in a using stm...

Can any one provide me a sample of Singleton in c++?

I write a singleton c++ in the follow way: class A { private: static A* m_pA; A(); virtual ~A(); public: static A* GetInstance(); static void FreeInstance(); void WORK1(); void WORK2(); void WORK3(); } } A* A::GetInstance() { if (m_pA == NULL) m_p...

Storing static user data in a C# windows application.

I have an application that needs to hit the ActiveDirectory to get user permission/roles on startup of the app, and persist throughout. I don't want to hit AD on every form to recheck the user's permissions, so I'd like the user's role and possibly other data on the logged-in user to be globally available on any form within the applica...