singleton

Is Guice needed in unit test?

I was told the Guice is aim to produce testable code, and not needed in the unit test. But how can I test a Singleton(@Singleton) without use Guice? ...

How to code Jon Skeet's Singleton in C++?

On Jon's site he has thisvery elegantly designed singleton in C# that looks like this: public sealed class Singleton { Singleton() { } public static Singleton Instance { get { return Nested.instance; } } class Nested { // Explicit static constructor to tell C#...

OO PHP where to reference singleton class?

Hi, I am going to use singleton classes to manage both DB connections and references to application settings. It seems a little messy to have to use the following code in every method in order to access the db class. $db = DB::getInstance(); Is there a more efficient way of going about it? Any advice appreciated. Thanks ...

OOPHP suitable alternative to singleton pattern?

Hi, I'm currently working on an oophp application. I have a site class which will contain all of the configuration settings for the app. Originally, I was going to use the singleton pattern to allow each object to reference a single instance of the site object, however mainly due to testing issues involved in this pattern, I've decided t...

Doesn't Passing in Parameters that Should Be Known Implicitly Violate Encapsulation?

I often hear around here from test driven development people that having a function get large amounts of information implicitly is a bad thing. I can see were this would be bad from a testing perspective, but isn't it sometimes necessary from an encapsulation perspective? The following question comes to mind: http://stackoverflow.com/...

Trouble with initializing NSMutableArray in my Singleton

Hey guys. I'm getting a weird error, and I can't figure it out. This takes place inside of a class that is created with the singleton pattern: - (NSMutableArray *) getCurrentClasses { NSMutableArray *current_classes = [[NSMutableArray init] alloc]; NSLog([NSString stringWithFormat:@"%d", [current_classes count]]); ... } ...

Should I use a singleton?

This is a semi-related question to question to the following question I just raised: http://stackoverflow.com/questions/1671259/utility-classes-good-or-bad After determining that a class is to act as a cache for Url parsing rules stored in an XML file, I have considered that a singleton would solve my problem, but introduce global stat...

how to initialize a Singleton?

Hello. Sometimes there is a need to initialize the singleton class with some helper values. But we can't "publish" a constructor for it. What is the workaround for this? Attention! overloading the GetInstance or setting a color is not my idea. The color should be set only once. I would like to be sure that MyPainter paints ONLY with th...

Python: @staticmethod with @property

I want Stats.singleton.twitter_count += 1 and I thought I could do class Stats: singleton_object = None @property @staticmethod def singleton(): if Stats.singleton_object: return Stats.singleton_object Stats.singleton_object = Stats() return Stats.singleton() But it throws an exc...

Simple C++ logger by using singleton pattern

Due to the flooding examples of implementing logger using Singleton pattern, I just written a simple C++ logger in the same approach for my program. However, since the famous double-checked locking approach is known to be no more thread-safe, I wonder if I should: 1) Forget about the use of Singleton pattern in this case? 2) Continue t...

C++ Threadsafe Singleton (NOT FOR INIT)

So I want to access a singleton class from multiple threads. Conceptually I'd think that calling non-const methods on this singleton instance would be not thread-safe. I've been looking online and no one seems to address this possible issue. Is there an actual problem with this, is the only issue with Singleton's thread-safety, the initi...

Java: unable to access static singleton method

hi. i am facing one problem.i have a class named "ReportingService" and it should be singleton and is is extending "CommonService". package MyApp.Services.ReportingService; public class ReportingService extends CommonService { private static ReportingService instance = null; public static ReportingService getInstance() { ...

C# - same control on two forms ?

Hi, I have extended a PictureBox and created a singleton. Is it possible to display the same instance of a PictureBox control on two distinct form same time ? Thanks in advance. ...

What are SingletonMethods and InstanceMethods in Ruby

I see a lot of stuff include ActiveRecord::XXXX::InstanceMethods extend ActiveRecord::XXXX::SingletonMethods I am unaware of the property or there working, just wanted a easy to understand answer. if it has a good reason to be used. ...

Help me remove a Singleton: looking for an alternative

Background: I have some classes implementing a subject/observer design pattern that I've made thread-safe. A subject will notify it's observers by a simple method call observer->Notified( this ) if the observer was constructed in the same thread as the notification is being made. But if the observer was constructed in a different thread,...

Singletons or non-singletons wrapped by a Singleton

Hi Guys. I have a singleton class called SingletonController1. This SingletonController1 instantiates a bunch of others Singleton classes. SingletonController1{ Authenticator - Singleton; DBAccessor - Singleton; RiskAccessor - Singleton; } My question is, what if I rework this design to: SingletonController2{ Authenticator -...

lazy initialization of singletons

While reading Jon Skeet's article on singletons in C# ( http://www.yoda.arachsys.com/csharp/singleton.html ) I started wondering why we need lazy initialization in a first place. It seems like the fourth approach from the article should be sufficient, here it is for reference purposes: public sealed class Singleton { static readonly...

php singleton per object with distinct identifier

This is something I was thinking about the other day. I want to have a singleton-like object. Instead of a single instance of a class, I want to have a single object with a matching variable. For instance. an existing employee object has a employee_id = 100 the getEmployee static method is called with employee_id = 100, i want to ret...

How do I make 15 static PHP classes work together well?

I have a personal PHP application that has about 15 classes. Each class is only initiated once as a page is executed. In other words, when the page loads: 15 classes are loaded, and as each class file gets loaded, I create one instance of the class. The application (so far) is designed so every variable in the system has one state duri...

Should WCF service typically be singleton or not?

I believe Jimmy Nillson said he generally made his webservices singletons. Is this the preferred method, and with WCF? Other than making the service methods static, is there something else to be done? ...