singleton

Problems with Singleton Pattern

I've been reading about Singleton pattern for last few days. The general perception is that the scenarios where it is required are quite few (if not rare) probably because it has its own set of problems such as In a garbage collection environment it can be an issue with regards to memory management. In a multithreaded environment it ca...

PHP: Use variable name to call static function on Singleton Object

I need to call a static function from an object using the Singleton design, but using a variable as the class name. The best way, $class::getInstance();, is only available in PHP 5.3, and the other way I found, call_user_func(array($class, 'getInstance'));, results in the maximum execution time being breached. Does anyone know why this ...

Singleton - Why use classes?

Just the other day I have seen code that uses the so called singleton pattern. Meaning something along the lines of class MySingleton{ public: void foo() { ... } static MySingleton return singleton } private: MySingleton(){ ... } ~MySingleton(){ ... } int bar; }; I do see why one would want to do that: ...

[C++] How do I implement a "single instance"-like design?

Hi, I'm writing an application which will run as a daemon. UIs will connect to it over TCP. Now, there is a class called UiTcpInterface which will handle all communication between the UI and this daemon. Now, I'm faced with the problem of ensuring there is only one instance of UiTcpInterface. What would be the best way to do it? Curren...

How to avoid multiple instances of windows form in c#

How to avoid multiple instances of windows form in c# ?? i want only one instance of the form running. Because there are chances of opening the same form from many pages of my application. ...

Single instantiation with arguments

I have a class (RInterfaceHL) that calls another class (JRIEngine) which provides native methods on a single-threaded application. Therefore I only want to have a single instance of my class (RInterfaceHL) per JVM. I can use the singleton pattern with static initialization to ensure only a single instantiation of RInterfaceHL, but RInt...

Creating a singleton in Delphi using the new features of D2009 and D2010

I'm looking to create a singleton in Delphi. I've done this before using older versions of Delphi, and ended up using global variables (in the implementation section) and using initialization and finalization to take care of the instance. Also there was no way of preventing the user from creating an instance as you couldn't hide the stan...

Singletons and DbSimple

Hi, i'm using DbSimple, but there is some code which i could write into another module. Here is it's code: <?php require_once 'config.php'; require_once 'DbSimple/Generic.php'; class MysqlWorker { private static $instance = NULL; public function getInstance() { if( self::$instance == NULL ) { s...

PHP and singletons again.

Hi, i have code: class ConfigReader { private static $instance = NULL; protected $configData = array(); public function getInstance() { if( self::$instance == NULL ) { self::$instance == new ConfigReader(); } ...

Singleton - if or try/catch ?

I've been sitting on this idea for quite a long time and would like to hear what you guys think about it. The standard idiom for writing a singleton is roughly as follows: public class A { ... private static A _instance; public static A Instance() { if(_instance == null) { _instance = new A(); } retu...

FluentNHibernate Session Management in ASP.NET

New to NHibernate(my disclaimer). I came across a similar and interesting article regarding MVC, however, I'm more curious as to what general best practices are for managing NHibernate sessions within a generic web application. I've come across the Burrow project, but I'm starting to realize there seems to be a few different directions ...

Singleton Pattern with Public Constructor

public class MySingletonClass { public MySingletonClass() { _mySingletonObj = Instance(); } public static MySingletonClass Instance() { if (_mySingletonObj == null) { lock (typeof(lockObject)) { if (_mySingletonObj == null) _mySingletonObj = new MySingletonClass(); } } ...

Using Singleton vs Single Call in .NET Remoting?

Up until this point all the .NET remoting code I have written or worked with has been exposed as SingleCall. I ran across a .NET remoting component hosted in a Windows service that is exposed as a Singleton. This object has the potential to be called by more than one client at the same time, and it has no locks or other provisions to p...

Singleton when using MarkUpExtension

How to create a singleton class which inherit from MarkUpExtension class? ...

Expiring Singleton instance after period of time

Putting aside, for now, the arguments about the relative virtues and disvirtues of the Singleton pattern, and considering that a Singleton is typically considered to be an instance that persists for the lifetime of an application, what would be the best way to go about having a Singleton that has a limited life? Is there anything amiss ...

How to pass global values around (singleton vs. ???)

I've inherited a project that stores various parameters either in a config file, the registry and a database. Whoever needs one of these parameters just reads (and in some cases writes) it directly from the store. This, or course, is stupid, so my first thought was to refactor the existing code so that the client doesn't know where the p...

Singleton Design Pattern: Pitfalls

Currently I have been very interested in this "design pattern". I am not sure though if there are downfalls using this strict global state implementation. So, when do you think not to practice singleton in an application? ...

iphone singleton instance in the LocateMe sample code

Can anyone explain why a singleton instance of the MyCLController class is used in the sample iphone app LocateMe found here as well as the overriding of retain, retainCount, release, and autorelease? Any help would be very much appreciated!! ...

Singleton Pattern in .Net is not possible?

Hi Guys I have a very interesting situation , i have figured out that Singleton pattern is not all possible with .net framework (Any version) look at this code below namespace SingletonPattern { class Singleton { private static readonly Singleton instance = new Singleton(); private static int mcount = 0; private ...

C++ singleton vs. global static object

A friend of mine today asked me why should he prefer use of singleton over global static object? The way I started it to explain was that the singleton can have state vs. static global object won't...but then I wasn't sure..because this in C++.. (I was coming from C#) What are the advantages one over the other? (in C++) ...