singleton

some confusions to singleton pattern in PHP

Hi all, In my team I've been told to write resource class like this style: class MemcacheService { private static $instance = null; private function __construct() { } public static function getInstance($fortest = false) { if (self::$instance == null) { self::$instance = new Memcach...

Singleton pattern in C++

I have a question about the singleton pattern. I saw two cases concerning the static member in the singleton class. First it is an object, like this class CMySingleton { public: static CMySingleton& Instance() { static CMySingleton singleton; return singleton; } // Other non-static member functions private: CMySinglet...

Is it good, that every PHP class implements a Singleton pattern

Hello, is it good, that every PHP class implements a Singleton pattern? I think, it will be less memory usage because of it. Is it right opinion? Thanks! ...

Elegant and 'correct' multiton implementation in Objective C?

Would you call this implementation of a multiton in objective-c 'elegant'? I have programmatically 'disallowed' use of alloc and allocWithZone: because the decision to allocate or not allocate memory needs to be done based on a key. I know for sure that I need to work with only two instances, so I'm using 'switch-case' instead of a map....

Singleton class in DLL used on multiple virtual directories

I have the following situation: multiple virtual directories under same application pool in IIS copy of same DLL in all those directories (same version number) a singleton class in one in this DLL The question is, is this singleton class created only once for all those Virtual Directory instances or is there for each of these directo...

Syntax error in Singleton Template class when porting from linux to windows

I have a singleton library from a linux project that I'm trying to port to Windows. When I try to compile, it errors with syntax error: identifier "rpSingleton" The error is from the following section of code: template <typename T> inline T& Q::Singleton<T>::Instance() { Singleton<T>*& rp_singleton(rpSingleton()); //ERRORS...

Are strings pooled in Python

Does Python have a pool of all strings and are they (strings) singletons there? More precise, in the following code one or two strings were created in memory: a = str(num) b = str(num) ? ...

singleton pattern in java- lazy Intialization

public static MySingleton getInstance() { if (_instance==null) { synchronized (MySingleton.class) { _instance = new MySingleton(); } } return _instance; } 1.is there a flaw with the above implementation of the getInstance method? 2.What is the difference between the two implementations.? public static synchronized MyS...

Is Structuremap singleton thread safe?

Hi, Currently I have the following class: public class PluginManager { private static bool s_initialized; private static object s_lock = new object(); public static void Initialize() { if (!s_initialized) { lock (s_lock) { if (!s_initialized) { // initialize ...

How to make a Scala Applet whose Applet class is a singleton?

Hi, I don't know if a solution exists but it would be highly desirable. I'm making a Scala Applet, and I want the main Applet class to be a singleton so it can be accessed elsewhere in the applet, sort of like: object App extends Applet { def init { // do init here } } Instead I have to make the App class a normal instantiatab...

How thread-safe is enum in java?

Hi, How thread-safe is enum in java? I am implementing a Singleton using enum (as per Bloch's Effective Java), should I worry at all about thread safety for my singleton enum? Is there a way to prove or disprove that it is thread safe? // Enum singleton - the preferred approach public enum Elvis { INSTANCE; public void leaveTh...

Can I use an abstract class instead of a private __construct() when creating a singleton in PHP?

When creating a Singleton in PHP, I ensure that it cannot be instantiated by doing the following: class Singleton { private function __construct() {} private function __clone() {} public static function getInstance() {} } However, I realised that defining a class as 'abstract' means that it cannot be instantiated. So is ...

Testing the existence of single instance -Singleton-C#

How to write test in Nunit to test that only one instance is created by my singleton class. ...

Singleton Objects causing error in session: singleton can't be dumped in Ruby

Im using Ruby on Rails and im adding an array of users into a session object. The problem is when I add it to the session I get the following error Status: 500 Internal Server Error singleton can't be dumped The problem is my user class called Expert is not a singleton class. The following is a snippet of my controller code. if @exp...

Multiple Instances of Static Singleton

I've recently been working with code that looks like this: using namespace std; class Singleton { public: static Singleton& getInstance(); int val; }; Singleton &Singleton::getInstance() { static Singleton s; return s; } class Test { public: Test(Singleton &singleton1); }; Test::Test(Singleton...

boost pool_alloc

Why is the boost::fast_pool_allocator built on top of a singleton pool, and not a separate pool per allocator instance? Or to put it another way, why only provide that, and not the option of having a pool per allocator? Would having that be a bad idea? I have a class that internally uses about 10 different boost::unordered_map types. If...

Boost C++ Singleton error LNK2001: unresolved external symbol "private: static long Nsp::HL::flag" (?flag@HL@Nsp@@0JA)

I try to create a multi-threaded singleton pattern class. Header: class HL{ public: static HL* getInstance(); ......... private: static HL* instance; static boost::once_flag flag; HL(); static void initOnce(); } CPP: HL* HL::instance = NULL; HL* HL::getInstance(){ if(instance...

Managing string resources in a Java application - singleton?

I seek a solution to the age-old problem of managing string resources. My current implementation seems to work well, but it depends on using singletons, and I know how often singletons can be maligned. The resource manager class has a singleton instance that handles lookups in the ResourceBundle, and you use it like so: MessageResource...

Singleton by Jon Skeet clarification

public sealed class Singleton { Singleton() { } public static Singleton Instance { get { return Nested.instance; } } class Nested { // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static Nested() ...

Purpose of singletons in programming

This is admittedly a rather loose question. My current understanding of singletons is that they are a class that you set up in such a way that only one instance is ever created. This sounds a lot like a static class to me. The main differnce being that with a static class you don't / can't instance it, you just use it such as Math.pi()....