singleton

PDO using singleton stored as class properity

Hi again. OOP drives me crazy. I can't move PDO to work. Here's my DB class: class DB extends PDO { public function &instance($dsn, $username = null, $password = null, $driver_options = array()) { static $instance = null; if($instance === null) { try { $instance =...

How to bind a control to a singleton in Cocoa?

I have a singleton in my FTP app designed to store all of the types of servers that the app can handle, such as FTP or Amazon S3. These types are plugins which are located in the app bundle. Their path is located by applicationWillFinishLoading: and sent to the addServerType: method inside the singleton to be loaded and stored in an NSMu...

Preventing call of a private constructor from within the class in java

Hi We can restrict the creation of object of a class by making its constructor private. But this constructor could still be called from within the class. Is there any way to prevent this in Java? Thanks. ...

Singleton object in IIS Web Garden

I have a lot of Singleton implementation in asp.net application and want to move my application to IIS Web Garden environment for some performance reasons. CMIIW, moving to IIS Web Garden with n worker process, there will be one singleton object created in each worker process, which make it not a single object anymore because n > 1. c...

Question about singleton property

I'm reading the java tutorial for enums located here and have a question: http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html#Card The part i'm confused about is as follows: "The Card class, above, contains a static factory that returns a deck, but there is no way to get an individual card from its rank and suit....

Cocoa Singleton conventions

Cocoa is full of singletons. Is there a logical/conventional difference between when the Cocoa APIs use NSSingletonObject *so = [NSSingletonObject defaultSingleton]; versus NSSingletonObject *so = [NSSingletonObject sharedSingleton]; ? Not a huge thing, but I don't really see why sometimes one is used versus the other. ...

Thread safety in Singleton

I understand that double locking in Java is broken, so what are the best ways to make Singletons Thread Safe in Java? The first thing that springs to my mind is: class Singleton{ private static Singleton instance; private Singleton(){} public static synchronized Singleton getInstance(){ if(instance == null) instan...

Using Dispose on a Singleton to Cleanup Resources

The question I have might be more to do with semantics than with the actual use of IDisposable. I am working on implementing a singleton class that is in charge of managing a database instance that is created during the execution of the application. When the application closes this database should be deleted. Right now I have this de...

using a Singleton to pass credentials in a multi-tenant application a code smell?

I'm currently working on a multi-tenant application that employs Shared DB/Shared Schema approach. IOW, we enforce tenant data segregation by defining a TenantID column on all tables. By convention, all SQL reads/writes must include a Where TenantID = '?' clause. Not an ideal solution, but hindsight is 20/20. Anyway, since virtually e...

Are multiple DAOs with jdbctemplate constructor injection a multi-threading risk?

I've got a large multi-threaded webapp in which I am passing in jdbcTemplates into DAO classes via constructor injection. Is this a multi-threading risk? Should I be passing in just the datasource instead? ...

Spring - singleton problem - No bean named '....' found

Hey, I can't figure out what is wrong with this beans definition. I'm getting this error http://pastebin.com/ecn5SWLa . Especially the 14th log message is interesting. This is my app-context file http://pastebin.com/dreubpRY httpParams is a singleton which is set up in httpParamBean and then used by tsccManager and httpClient. The var...

iPhone noob - setting NSMutableDictionary entry inside Singleton?

Yet another iPhone/Objective-C noob question. I'm using a singleton to store app state information. I'm including the singleton in a Utilities class that holds it (and eventually other stuff). This utilities class is in turn included and used from various view controllers, etc. The utilities class is set up like this: // Utilities.h...

Critical section initialization when creating thread-safe singleton (C++)

I'm trying to do the same thing as suggested in this solution: http://stackoverflow.com/questions/164496/how-can-i-create-a-thread-safe-singleton-pattern-in-windows/164640#164640 But, where should the critical section be initialized and uninitialized? ...

Interview question: about Java serialization and singletons

In an interview the interviewer asked me the following question: is it possible to serialize a singleton object? I said yes, but in which scenario should we serialize a singleton? And is it possible to design a class whose object can not be serialized? ...

Add multiple entities to Javascript namespace from different files

Given a namespaces ns used in two different files: abc.js ns = ns || (function () { foo = function() { ... }; return { abc : foo }; }()); def.js // is this correct? ns = ns || {} ns.def = ns.def || (function () { defoo = function () { ... }; return { deFoo: defoo }; }()); Is this the proper way to a...

Tomcat, Singletons, & ServerContextListener

I've been attempting to pull hard-coded configuration data from a Tomcat web-app which I'm working on, into JNDI (web.xml) to make the app more configurable. Instead of continuously doing lookups into the InitialContext, I'm caching the lookups and the InitialContext using a singleton. Of course, when I stop and start the application...

Why use singleton instead of static class?

When would a singleton actually be easier or better than a static class? It seems to me creating a singleton is just extra effort that's not actually needed, but I'm sure there is a good reason. Otherwise, they wouldn't be used, obviously. ...

Thread-safe initialization of static variables

I've been using this pattern to initialize static data in my classes. It looks thread safe to me, but I know how subtle threading problems can be. Here's the code: public class MyClass // bad code, do not use { static string _myResource = ""; static volatile bool _init = false; public MyClass() { if (_init == tru...

How to implement singleton from one project to another project

i am developing windows form application . i need to implement single ton design project. when implementing the singleton in two classes which are in same package it works correctly but implementing the two classes in different package by justing creating object for both classes in that class. how can I implement that.here code ex publi...

Achieving C# "readonly" behavior in C++

Hi guys, this is my first question on stack overflow, so be gentle. Let me first explain the exact behavior I would like to see. If you are familiar with C# then you know that declaring a variable as "readonly" allows a programmer to assign some value to that variable exactly once. Further attempts to modify the variable will result in ...