singleton

Generating singletons

This might sound like a weird idea and I haven't thought it through properly yet. Say you have an application that ends up requiring a certain number of singletons to do some I/O for example. You could write one singleton and basically reproduce the code as many times as needed. However, as programmers we're supposed to come up with in...

Extending singletons in PHP

Hi good people. I'm working in a web app framework, and part of it consists of a number of services, all implemented as singletons. They all extend a Service class, where the singleton behaviour is implemented, looking something like this: class Service { protected static $instance; public function Service() { if (isset...

Singleton failure. Initiates another object in include_once(somescript.php)

Hello I'm using the PHP singleton from Wikipedia. The problem is the singleton has a method that loads and runs other PHP scripts via require once and when these script are run and implement the Class method get_shared_instance a new singleton is initiated. Why is this? And what is the work around? the singleton (in a basic form): cla...

Is this Singleton implementation correct and thread-safe?

Is this singleton implementation correct and thread-safe? class Class { public static readonly Class Instance; static Class() { Instance = new Class(); } private Class() {} } ...

C++ checking singleton pointers

Hello, I have an application, which has a (Qt C++) singleton logger class. The GetInstance() implementation is: if(m_Instance == NULL) { try { m_Instance = new Logger(); } catch(...){} } return m_Instance; Now I have following macro in the .h file: "#define LOG Logger::Instance()->Log" Everything is fine as l...

Qt C++: Global objects vs. reference chain

Hello, Currently I'm using singleton pattern for certain global objects in my application (Qt application for Symbian environment). However, because of some problems (http://stackoverflow.com/questions/3147375/c-checking-singleton-pointers/3147806#3147806) it looks like that I have to change the logic. I have 3 classes (logger, setting...

Unit testing with Spring framework singleton beans

It’s a general consensus that Singleton is bad for unit-testing. But what about having an IoC container like the Spring framework to control your beans which are singletons by default? Is using those beans in your classes also considered bad for unit testing the same way singleton is? ...

How can I use jUnit to test a class that uses a singleton?

I am developing a simulator for a machine. It reads a file in, stores it in a string (as a binary number) to an array that is supposed to simulate the memory of a computer. Then it gets passed to an interpreter that takes gets the first element of the array, and based on the first 4 characters it gets sent to another class. An example o...

Only 5 instances of a class

I want to have only 5 instance of a class throughout the application life time. How can I achieve this? Please give sample code, if possible. ...

Singleton Models Doctrine

Can we have a model class which is singleton in Doctrine? For Singleton classes I should have a private/protected constructor....but this is not possible as I am extending a Doctrine class which has a public constructor You can argue about the use of the Singleton pattern when interacting with the DB, but just consider this scenario: ...

Alternatives for the singleton pattern?

Hi there, I have been a web developer for some time now using ASP.NET and C#, I want to try and increase my skills by using best practices. I have a website. I want to load the settings once off, and just reference it where ever I need it. So I did some research and 50% of the developers seem to be using the singleton pattern to do t...

Design Pattern for cache management

I have to create a COM API which basically will read some data from XML , do some processing on it and return some data as a string. This API will be in COM DLL which will be in memory most of times. I have created a struct to hold the data in memory which can be used as cache so as to avoid reading file every time whenever API is call...

Can you use dependency injection everywhere a singleton is needed?

This may sound like a stupid question, but can DI be used everywhere where a Singleton is needed? Or are there use cases where a Singleton makes more sense? A professor of mine said that there a few but valid cases where a Singleton is "good enough" but I'm somehow not happy with that :-/. ...

Make an enum INSTANCE private

I am using an enum singletom pattern like this: public enum LicenseLoader implements ClientLicense { INSTANCE; /** * @return an instance of ClientLicense */ public static ClientLicense getInstance() { return (ClientLicense)INSTANCE; } ...rest of code } Now I want to return the Interface and hide...

C#: using type of "self" as generic parameter?

This may seem a bit odd, but I really need to create a workaround for the very complicated duplex - communication - handling in C#, especially to force other developers to observe the DRY - principle. So what I'm doing is to have a type based multiton that looks like this: internal sealed class SessionManager<T> where T : DuplexService...

Attach .net app to another process

I have a .net Console application that uses a COM component to generate PDF. Basically it prints the input document as PDF and is configured as a printer in the Desktop. Now, this component is singleton by design and does not support multiple instances. My application works fine if this process is not already running. However, if an i...

Singleton and Client Activated Object(CAO), Remoitng

Both perform the same operation that is MSDN Singleton: ""Single Call objects service one and only one request coming in.... " CAO: "Client-activated objects (CAO) are server-side objects that are activated upon request from the client...." in both cases, data is not shared, but in singleton only once client can be connected at a ti...

Good way to access Spring singleton from within domain objects?

I have a domain model that consists of fairly large object graphs, where domain objects are creating other domain objects and so forth. Each of these domain objects needs access to a small handful of singleton-type helper objects for various purposes. I was about to implement them using the Java singleton pattern when I remembered t...

Where exactly the Singleton Pattern is used in real application ?

Hello Everyone, I was just curious where exactly the singleton pattern is used... I know how the pattern works and where it can be used but i personally never used in any real application. Can some one give an example where it can be used.. I would really appreciate if some one can explain how and where they have used in real application...

Return same instance for multiple interfaces

I'm registering components with the following code: StandardKernel kernel = new StandardKernel(); string currentDirectory = Path.GetDirectoryName(GetType().Assembly.Location) foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies()) { if (!Path.GetDirectoryName(assembly.Location).Equals(currentDirectory)) continue;...