singleton

Portable thread-safe lazy singleton

Greetings to all. I'm trying to write a thread safe lazy singleton for future use. Here's the best I could come up with. Can anyone spot any problems with it? The key assumption is that static initialization occurs in a single thread before dynamic initialisations. (this will be used for a commercial project and company is not using boo...

Different ways to write singleton in Java

The classic of writing a singleton in java is like this: public class SingletonObject { private SingletonObject() { } public static SingletonObject getSingletonObject() { if (ref == null) // it's ok, we can call this constructor ref = new SingletonObject(); return ref; } priv...

C++: duplicated static member?

I have a class which needs to be a singleton. It implemented using a static member pointer: class MySinglton { public: static MySinglton& instance() { ... } private: static MySinglton* m_inst; }; This class is compiled into a .lib which is used in multiple dlls in the same application. The problem is that each dll sees a diffe...

Unity framework with configuration file and singleton classes.

Hi, so the store goes like this. I have singleton class (a.k.a ServiceLocator) which you can get the instance using the "CreateInstance()" method. At the same time, we added Unity into our application and configure it using the standard configuration file. The problem started when i was trying to map interface IServiceLocator to get th...

Iphone: label text udates value but does not show in view

Hi.. Im working on a part of code where a label text should change value after getting a new value from a singleton class. Seems simple enough, and after testing the value it actually changes aswell, but its not updated in the view. In the view it stays the same as before. Ive tried the code both in -(void)viewDidLoad and -(void)viewWi...

javascript singleton question

I just read a few threads on the discussion of singleton design in javascript. I'm 100% new to the Design Pattern stuff but as I see since a Singleton by definition won't have the need to be instantiated, conceptually if it's not to be instantiated, in my opinion it doesn't have to be treated like conventional objects which are created f...

PHP stdClass() with __get() Magic Method

Take the following code as an example: class xpto { public function __get($key) { return $key; } } function xpto() { static $instance = null; if (is_null($instance) === true) { $instance = new xpto(); } return $instance; } echo xpto()->haha; // returns "haha" Now, I'm trying to archive the...

To update UIProgressView for Multiple File Download (how to use delegate)?

I am trying to update the multiple file download progress value to UIProgressView on a table cell. I have the FileDownloader class which has NSOperationQueue that does asynchronous download operations. I am thinking to update the UI using a "delegate" from FileDownloader class. But I cannot compile the codes. I have FileDownloader as ...

Android: Accessing single database from multiple activities in application?

I have a todo list type application that stores all of the note data in a sqlite3 database. Each activity in the application needs access to the database to edit different parts of the data in real time. Currently, I have each activity open its own DBManager object (the helper class I created to manage the database). This is causing p...

How to test a Singleton class?

Hi, I am using RSpec and want to test the constructor of a Singleton class more than one time. How can I do this? Best regards ...

Getting object instance by string name in scala

I need the object (or "singleton object" or "companion object"... anything but the class) defined by a string name. In other words, if I have: package myPackage object myObject ...then is there anything like this: GetSingletonObjectByName("myPackage.myObject") match { case instance: myPackage.myObject => "instance is what I wanted"...

Can I make my MXML Component a Singleton?

I have an MXML component in a website that I am reusing in a few different screens. Based on what the user clicks, the component initializes a few values and starts playing media. Now if I click through the site, and I play media in screen 1, the component initializes fine and plays the media. If I then go to screen 2 and play the medi...

Singleton or not

Hi there.. I have a windows service running.Inside this service I have hosted some service (WCF). I need to have some kind of a "in memory data holder" class. The purpose of this class is to hold not-persistant data as long as the windows service is running. This class must be accessible thru the WCF services. They put some values in t...

StructureMap singleton behavior not working

My code is public static class ContainerBootstrapper { public static void BootstrapStructureMap() { ObjectFactory.Initialize(x => x .ForRequestedType<ValueHolder>() .CacheBy(InstanceScope.Singleton) .TheDefaultIsConcreteType<ValueHolder>()); } } In...

Object that is needed throughout the application

I have an application that gets some data from the user when the application loads and is needed throughout the application, what is the best way to keep hold of the object that holds this data throughout the entire lifetime of the application? This data needs to be available to most other objects created during the lifetime of the appl...

Singleton python generator? Or, pickle a python generator?

I am using the following code, with nested generators, to iterate over a text document and return training examples using get_train_minibatch(). I would like to persist (pickle) the generators, so I can get back to the same place in the text document. However, you cannot pickle generators. Is there a simple workaround, so that I can sa...

Singleton to read properties file in a Java webapp; correct approach?

Hi, all. My spaghetti monster consumes XML from several different SOAP services, and the URL for each service is hardcoded into the application. I'm in the process of undoing this hardcoding, and storing the URLs in a properties file. In terms of reading the properties file, I'd like to encompass that logic in a Singleton that can be ...

Nested Singleton Class

Is it possible to nest a singleton class inside a non-singleton class in C#, and if so, are there any restrictions to the life-cycle of the singleton in this case? public class NonSingletonClass { public NonSingletonClass() { // Initialize some stuff. } // Put some methods here. public class SingletonClass { // Sin...

Best practice for data access from text file in iPhone app?

OK. I have an application which is using data that is stored in text files. The data is such that the application's users will drill down through a couple of levels of nested table views to get to the data that they need to access and alter, at which point any changes will be written back to the text files. As the drilling down through...

The need for volatile modifier in double checked locking in .NET

Multiple texts say that when implementing double-checked locking in .NET the field you are locking on should have volatile modifier applied. But why exactly? Considering the following example: public sealed class Singleton { private static volatile Singleton instance; private static object syncRoot = new Object(); private Sing...