singleton

Warning signs for untestable code

Code that is untestable really annoys me. The following things make oo-code untestable: global states, e.g., the Singleton Design Pattern static methods that do some fancy work e.g database access deep inheritance tree work in constructor e.g. control statements classes that violate the Single Responsibility Principle Are there more ...

Java Enum Singleton

Building on what has been written in SO question Best Singleton Implementation In Java and specifically talking about using enum to create a singleton, what are the differences/pros/cons between (constructor left out) public enum Elvis { INSTANCE; private int age; public int getAge() { return age; } } and the...

Singleton in PHP

am working on a web app in PHP that requires users to register and login with their credentials. However, i am using the singleton pattern in all my PHP class files. I have something bothering my mind that i would like to clarify. For instance, When the application goes live and we have several users on the site at the same time, doing...

smalltalk singleton pattern: how do I initialize the instance variables?

I'm having trouble in getting the singleton pattern to initialize a instance variable in smalltalk. (here is a link to another implementation for clarification) this is what I have: new ^UniqueInstance ifNil: [UniqueInstance := self basicNew. UniqueInstance: instanceVar := Object new. ]. that last line (UniqueInsta...

Singleton DAL class

Hi, Shouldn't classes in DAL (customerDAL) be singleton? Since my controllers (customerController) exposes "Shared Subs", then on every call, there is no need to create a new DAL object if already existed. Correct? Thanks ...

Ways for lazy "run once" initialization in Java with override from unit tests

I'm looking for a piece of code which behaves a bit like a singleton but isn't (because singleton's are bad :) What I'm looking for must meet these goals: Thread safe Simple (Understand & use, i.e. few lines of code. Library calls are OK) Fast Not a singleton; for tests, it must be possible to overwrite the value (and reset it after th...

How to create .NET interface with static members?

In .NET 3.5, I'd like to create a singleton interface: interface ISingleton <T> { public static T Instance {get;} } Of course that doesn't work but is what I'd like. Any suggestions? EDIT: I just want it to be known that all singeltons will have a static property named Instance of the class type. It is always there. An interface w...

C++ singleton GetInstance() return

When implementing a singleton in C++, is it better for GetInstance() to return a pointer to the singleton object, or a reference? Does it really matter? ...

Singleton instance declared as static variable of GetInstance method

I've seen implementations of Singleton patterns where instance variable was declared as static variable in GetInstance method. Like this: SomeBaseClass &SomeClass::GetInstance() { static SomeClass instance; return instance; } I see following positive sides of this approach: The code is simpler, because it's compiler who respon...

Are there cases where a singleton is the best option?

There is a lot of discussion on this site about why singletons should be avoided but are there any cases where a singleton makes sense and is the best option? An example where I think that a singleton is the best option is when implementing an Identity Map where you need to keep track of all models that have already been loaded from the...

Singleton with finalizer but not IDisposable

This is what I understand about IDisposable and finalizers from "CLR via C#", "Effective C#" and other resources: IDisposable is for cleaning up managed and unmanaged resources deterministically. Classes that are responsible for unmanaged resources (e.g. file handles) should implement IDisposable and provide a finalizer to guarantee th...

Java Threads: Specifying what should be executed in the run function

I have a DBAdmin class that connects to the database, and then some other classes such as Article, Category and so on, that perform certain queries to a database. I am using these classes in a Swing application to build a sort of small Intranet CMS application. Now, the Category class has a bunch of static methods such as addCategory, ...

Class Access question

I know this should be simple and I should know it but it's eluding me for the time being. I am using a singleton pattern to help with logging stuff. However, logging only happens in one class, and the singleton is basically a watcher for a boolean that opens and closes the log file. Because I don't want the file to be opened more than o...

Is this a legitimate use of a Singleton?

(Disclaimer: This question is not specific to ASP.NET) I have a control which may be templated, similar to the login controls: public abstract class TemplatedControl : CompositeControl { public ITemplate Template { get; set; } protected override void CreateChildControls() { var template = this.Template ?? CreateDef...

How should I refactor my code to remove unnecessary singletons?

I was confused when I first started to see anti-singleton commentary. I have used the singleton pattern in some recent projects, and it was working out beautifully. So much so, in fact, that I have used it many, many times. Now, after running into some problems, reading this SO question, and especially this blog post, I understand the e...

Getting Spring Application context from a non bean object without using Singleton

I need to get the spring application context from a non bean object. In another thread in SO, the accepted answer suggests to use singleton to get the application context. Getting Spring Application Context But using singleton makes my code more coupled and less testable, the usual problems discussed in many threads (e.g. What is so bad...

Share static singletons through EJB's

I'm trying to create a cache in a webservice. For this I've created a new Stateless Bean to provide this cache to other Stateless beans. This cache is simply a static ConcurrentMap where MyObject is a POJO. The problem is that it seems as there are different cache objects. One for the client beans, and another locally. -CacheService -Ca...

Multiple DB connection class

How do you create multiple DB connections using Singleton pattern? Or maybe there's better approach, to share the same class but multiple connections? ...

Global vs Singleton in .NET

I have a very common situation here. And for years I haven't found if what i am doing is RIGHT by industry standards.Consider an application which connects to the database, but where the connection string instead of being stored in some file/setting is being passed as a command line parameter at startup OR the database is browsed to at t...

Don't static members make classes kind of (global) objects themselves?

Every time I come across an implementation of the singleton pattern or any static classes (i.e. classes with (almost) only static members) I wonder whether this isn't actually a hack and therefore heavy abuse of the principle of classes and instances just to design single objects instead of designing classes and creating a single instanc...