Questions tagged [singleton]

The singleton is a design pattern to ensure that exactly one application-wide instance of a particular class exists.

Filter by
Sorted by
Tagged with
3 votes
2 answers
13k views

Singleton implementation of a database connection

I have implemented as follows, a class applying singleton pattern to get a global single access to database. I intend to provide a thread-safe implementation. ...
Joan Dimko's user avatar
2 votes
0 answers
46 views

Classes to help transform "How hard is it to …?" questions into replies like "It's really hard to …"

I've written a Twitter bot, @answering_yelp, which responds to another Twitter bot, @hard_to_yelp. They scrape yelp reviews for sentences which start "How hard is it to..." and my bot answer with some ...
AJFaraday's user avatar
  • 172
0 votes
3 answers
4k views

C++ singleton using templates

Is this a good Singleton implementation? Is there anything I should be aware of? If so, how can it be improved? ...
BPL's user avatar
  • 157
5 votes
1 answer
1k views

Singletons with eager and lazy instantiation

I've found more than one implementation of the Singleton Pattern in Java (more like 5-6). I want to know if both of the following snippets of code (written by me) are valid implementations of ...
ovidiu-miu's user avatar
1 vote
1 answer
230 views

An improved single instancing library

I previously posted a question: Single instancing class and received some good feedback on it. I have implemented most of the changes suggested by @EBrown. I also implemented the suggestion by @...
TheLethalCoder's user avatar
0 votes
1 answer
77 views

PHP Singletons Wrapper (Dynamic Admin) [closed]

I need to create an administration board that allows devs to create or modify options and display them in a panel. The Admin needs to be the same on each call made to it. I've chosen to make all ...
wpmika's user avatar
  • 13
3 votes
1 answer
2k views

Log4j Singleton

I'm trying to design and develop a singleton class that will handle resolution and configuration of log4j (not log4j2). The basic idea is to have a single class, reusable in any application, that ...
lincolnadym's user avatar
1 vote
1 answer
640 views

Monitoring errors with singleton

I need to monitor my functions for "errors" and want to print the warnings at the end of the functions. I thought using singleton class could be a good idea in here (code inspired by this example): <...
Tim's user avatar
  • 111
1 vote
1 answer
107 views

Is this a good and simple example for Singleton Pattern [closed]

This is a scenario where we keep a single instance(as in Singleton Pattern) to get the configuration of an application. Below is the main class and i set values to the configuarion values here and ...
Harsha Jayamanna's user avatar
3 votes
2 answers
1k views

Caching all the prepared statements in thread safe way

I have a below Singleton class where in my getStatement method, I populate a CHM by doing if check. ...
david's user avatar
  • 1,647
4 votes
2 answers
444 views

Singleton Pattern + unavoidable Public Constructor

We have an object that needs to be converted to a different type before storing it in database. For that purpose, the library provides an annotation over the field that specifies the appropriate class[...
nitish712's user avatar
  • 141
13 votes
2 answers
6k views

Unit-testing friendly singleton

In my project we have a few singletons, which tends to be problematic in unit tests. So I wanted to find solution for the problem. Here is what I came with so far: smart_singleton.h ...
Marcin K.'s user avatar
  • 233
5 votes
2 answers
7k views

Header only c++ singleton pattern implementation

I want to implement the singleton pattern in a header only c++ library. (Please refrain from sharing your opinion on the pattern itself!) This would be trivial if I could rely on the compiler ...
bgp2000's user avatar
  • 153
4 votes
1 answer
244 views

JSON loader with event bus, cache, and session

I have an Android application which needs to retrieve JSON files from a server and then display the information in my app. To accomplish this I have the following architecture: The main activity adds ...
Adam's user avatar
  • 321
3 votes
3 answers
3k views

Base class for singletons

Even though singletons are bad practice and often unnecessary, I still see many developers using this pattern over and over. Since implementation of this pattern usually requires some common code, I ...
DevWL's user avatar
  • 179
1 vote
1 answer
342 views

Singleton Provider

I've taken over a code base that is still very much in it's infancy, and stumbled across this class. I'm just wondering if it's actually worth it to have a ...
Richard Ueckermann's user avatar
1 vote
1 answer
58 views

How can I extract these similar codes into only one method? [closed]

I have implemented three ways of Singleton idiom in Java, and now I want to write some test code to see there performance. Here is my code: ...
Novemser's user avatar
3 votes
1 answer
521 views

Storing global constants in app delegate or singleton

I am converting a non ARC projecs to ARC. I just found that after login in my old app, the data is stored in app delegate objects, like Home_DataArray, various ...
ChenSmile's user avatar
  • 183
1 vote
1 answer
1k views

Generic Singleton (Thread Safe)

LifeTracker.h ...
gaurav bharadwaj's user avatar
2 votes
0 answers
54 views

Java interface to C library - Refactoring to singleton usage

I am currently writing a Java interface to a C library. It mostly just delegates calls and adds a object oriented layer on top of it to make integration into a regular Java program more natural. The ...
Luca Fülbier's user avatar
4 votes
4 answers
274 views

Client to connect to server and show a text menu

First of all: Sorry for my English, that said... I am developing an obligatory for my University, so far it has a class that provides the user with options to connect to a server (once connected it ...
Alvaro Rodriguez Scelza's user avatar
3 votes
2 answers
3k views

Thread safe templatized singleton class

...
gaurav bharadwaj's user avatar
2 votes
3 answers
1k views

Settings class in C# application

I made a settings class for my own C# application. My main goal was to reach this class from every other class (this is why I choose the Singleton pattern). I also wanted to distinguish the different ...
Ryper's user avatar
  • 176
3 votes
0 answers
105 views

JavaScript Singleton Pattern/Class "Methods"

I have a library that will be called in multiple places. Here is my implementation: var vessel1 = new Vessel(); var vessel2 = new Vessel(); ...
Lansana's user avatar
  • 387
0 votes
1 answer
103 views

Generalizing the Initialization of Singleton Derived Classes

I have the following construct for storing and restoring my applications settings to and from XML. ...
MoonKnight's user avatar
-5 votes
1 answer
95 views

Does this work for Singleton Class concept? Trying to fill older HashTable once on login [closed]

I am trying to fill a hashtable with LicenseID and FullName when user first logs in. I don't have access to the Licenses database right now. So "Context" is a place holder. I am at loss if what I am ...
JustJohn's user avatar
  • 101
2 votes
1 answer
637 views

Composite implementation of a prototype singleton registry

I am studying design patterns and I've implemented a composite prototype pattern which is also a singleton (one instance per JVM). The main and most important part of the code is the ...
Levent Divilioglu's user avatar
2 votes
2 answers
126 views

ResourceManager class consisting of multiple singletons

I've implemented a class, which provides access to different static resources, which are used by different frontends. I'm hesitating, because in Java getters and setters are quite wide spread. So my ...
Robin's user avatar
  • 137
0 votes
1 answer
2k views

Singleton manager/service classes with mockable dependencies in swift

In our swift project, there are multiple service classes that encapsulate functions, mostly network-api calls. They implement a protocol, and depend on other service classes, e.g. an authorization ...
Falco Winkler's user avatar
2 votes
1 answer
4k views

Singleton method for Redis connection in Python

Here's my class for providing a singleton instance of a Redis connection. What do you think? ...
John Bachir's user avatar
3 votes
2 answers
17k views

Simple singleton database connection pool

I'm studying design patterns, and to demonstrate a singleton, I've implemented a primitive database connection pool. ConnectionPool.java ...
Levent Divilioglu's user avatar
1 vote
0 answers
1k views

JavaScript to connect to Redis using Singleton

I'm trying to create a class to connect to Redis in Express. I don't think my code is good enough as I got variable db hanging out. ...
toy's user avatar
  • 831
1 vote
2 answers
1k views

Initialization lock for singleton

I'd like someone to double-check my use of Atomic primitives, because multi-threading is hard. What I don't want to do is locking the whole get method, or having too many pre-checks per get() call. <...
MLProgrammer-CiM's user avatar
1 vote
1 answer
2k views

Throwing exception in the singleton pattern in C#

I'm implementing an HTTP client shell that's going to be used for the lifetime of my application in C#. It needs to be initialized before the instance can be accessed. It looks something like this: ...
Jez's user avatar
  • 113
2 votes
1 answer
1k views

Singleton implementation of Cache Dependency Manager

I am trying to implement a singleton CacheDependencyManager, by combining this CacheDependencyManager from Steve Greatrex with a singleton pattern from Jon Skeet (example 6). I'd really appreciate ...
Martin Hansen Lennox's user avatar
-2 votes
1 answer
401 views

Enum Singleton implementation in Java

I just wanted to implement the enum singleton in java. Here is my implementation where I have tried to create a singleton DataSource instance: ...
Rahul's user avatar
  • 11
6 votes
1 answer
944 views

Generic singleton array with archiving and insertion functions

I find that I often have to maintain a singleton primarily to hold an array that is sometimes persisted between user sessions, that should have only one class of items, and that sometimes needs to be ...
sunny's user avatar
  • 1,825
3 votes
1 answer
437 views

Singleton Class for WordPress Plugin

I'm developing a WordPress plugin using the singleton pattern with the following requirements: There should ever exist one instance of the plugin Another instance of the object should never be ...
unity100's user avatar
  • 131
6 votes
3 answers
964 views

Loading Configurations from plist into singleton

In my iOS application, I've created a singleton class that reads a configuration plist file and provides accessor methods to easily retrieve the values: ...
W.K.S's user avatar
  • 512
3 votes
0 answers
221 views

Threadsafe singleton factory to use in Apache thrift server RPC system

My goal was to write a factory that the code did not have to be edited. Thus I am registering my factory functions into a singleton factory. I am going to be using this code in Apache thrift server ...
user249806's user avatar
1 vote
1 answer
98 views

Application Configuration in PHP

I'm learning PHP and I know too many ways to configure the application. I use this method: Folder: app config app.php db.php src class.php index.php ...
Nic Kji's user avatar
  • 11
3 votes
1 answer
340 views

(Y.E.S.E.J.) Yet Another Singleton with Enum in Java

I have a Java class which has to be generated only once for all the objects that I have - it is a small program. This singleton class holds a mapping of characters. I Googled stack overflow and found ...
Kadir Gunel's user avatar
-2 votes
1 answer
942 views

Configurations and interrupt handler for a microcontroller using a singleton as a static instance

In my project I need to have one class as singleton which must be globally accessible: board.h: ...
vlk's user avatar
  • 107
6 votes
1 answer
138 views

Android app with config and customization classes

I have an Android app with three different Activity classes. Each of the Activities uses and changes config and user-customization data stored in a wrapped ...
user5292387's user avatar
6 votes
2 answers
691 views

Singleton-ish RMI Server

In this program there is a single remote server which performs miscellaneous work. Because there is only one server, it seems like a singleton pattern could fit well. The problem that a distributed ...
flakes's user avatar
  • 1,925
6 votes
1 answer
106 views

Creating an in-memory ViewCounter that commits to database every 10 minutes

This singleton is responsible for counting every pageview (called Routes in our case) we get by id, until the timer runs out and commits the results to the database in one query. Then the counter gets ...
Thermometer's user avatar
2 votes
1 answer
1k views

Global.asax singleton

Please help me decide whether this should be good or bad idea to declare a Singleton variable in Global.asax file. (It is not actually singleton pattern, I just want to make sure that only one ...
Nikhil Chavan's user avatar
3 votes
1 answer
2k views

Singleton base class

I just wrote a singleton base class for a hobby project I'm currently working on. Python is not my first language, which is why I am not sure if everything is done the pythonic way. I also used ...
linluk's user avatar
  • 315
3 votes
2 answers
4k views

Manage Sessions within a Singleton

I am looking for advice if the below code is a good idea or not. If not, why? I am building a website which allows a user to purchase items. The user account and cart items will be stored in Session. ...
PhillyNJ's user avatar
  • 349
2 votes
1 answer
75 views

Javascript singleton to notify different objects of changes in viewport size

I'm working on a singleton object to manage size changes in the viewport, I wanted it to be a singleton because there may be different unrelated objects that need to be notified when a change in ...
Eduardo E. Valenzuela's user avatar