Skip to main content

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
67 votes
9 answers
29k views

Implementing a Singleton pattern in C#

Is this the best way to implement this pattern in C#? ...
Aim Kai's user avatar
  • 772
41 votes
7 answers
3k views

Is my code a 'safe' singleton?

I was wondering if my code will produce a true singleton. I am creating an Android app, and all activities should access my API through one instance of the SyncApi ...
jjnguy's user avatar
  • 661
38 votes
4 answers
30k views

Swift 1.2 Singleton Implementation

I am pretty new to Swift, as a solo developer I was wondering if somebody could pass comments on the singleton implementation below. The code does work, but being new to Swift and knowing that there ...
fuzzygoat's user avatar
  • 511
31 votes
1 answer
30k views

Single-instance WPF application

This is the code I implemented so far to create a single instance WPF application: ...
Tommaso Belluzzo's user avatar
22 votes
3 answers
66k views

Modern C++ Singleton Template

I recently read about the C++17 static inline member declaration and thought that this will make templates a little bit cleaner, since static members can now be ...
muXXmit2X's user avatar
  • 343
22 votes
2 answers
25k views

Thread-safe Singleton class

I have created this class to simulate a thread-safe Singleton. Have I missed anything? ...
q0987's user avatar
  • 321
18 votes
3 answers
44k views

Singleton using enum

Is this the correct implementation of a singleton using enum? ...
Nishant's user avatar
  • 477
17 votes
2 answers
2k views

Singleton Design Pattern implementation in a not traditional way

For implementing Singleton we can use Traditional way like this Article, but i think that to write it in another way: ...
pmn's user avatar
  • 273
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
12 votes
5 answers
11k views

Singleton Database class

Currently my team is building an application involving a database. We have to write a lot of data (approx 2,000,000 records) to the database and therefore we decided to open a connection once and ...
Dion V.'s user avatar
  • 529
12 votes
4 answers
5k views

Sound manager for Android

I created a singleton class for managing sound effects on Android. This class will only be instanced and loaded once at the beginning, and each activity will use the loaded songs. I don't know either ...
FR073N's user avatar
  • 231
12 votes
1 answer
139 views

Storing messages for the application's runtime

I am currently writing a well.. manual testing site for a chat-based bot. The full code can be found on github. For that purpose I had to keep track of the messages currently in the "system". The ...
Vogel612's user avatar
  • 25.2k
10 votes
1 answer
11k views

Can we now `implement Singleton` using Java 8?

After my success with extending enum - I thought I would try to make Singleton creation as simple as possible since Java-8 makes ...
OldCurmudgeon's user avatar
9 votes
4 answers
24k views

Threadsafe DBContext in singleton

I found out the hardway that access to DbContext in .NET is not threadsafe. I have a singleton for logging things using a dbcontext. The original version uses something like ...
Martijn's user avatar
  • 585
9 votes
3 answers
2k views

Storing a Ruby app's settings

I have used code which looks a little like this in an app I'm working on: ...
bluekeys's user avatar
  • 255
9 votes
1 answer
1k views

JavaScript Boilerplate - Review comments required

I am in between to create JavaScript Boilerplate (collection of best practices around) for low/medium complex project and will host on GitHub in sometime once finalized it. Have divided the ...
Mohammad Arif's user avatar
8 votes
3 answers
1k views

Singleton Database wrapper

Before everybody yells at me about why I wrote a PDO wrapper class, it's to avoid writing try/catch, ...
Xefa974290823499093's user avatar
7 votes
5 answers
7k views

A generic singleton

What do you think about this for a generic singleton? ...
Simon Hughes's user avatar
7 votes
2 answers
4k views

C++ shared_singleton

I actually feel bad posting "yet another singleton"... I wrote the following one many years ago and had recently found another application for it. We had many threads, each running the same function ...
screwnut's user avatar
  • 289
7 votes
1 answer
505 views

Prime Numbers Store

Let's say we need to create a store for selling prime numbers. Users enter the store and ask to buy a number. If the number asked is a prime number, 1.1. then it's either available for sale 1.2. ...
rycle's user avatar
  • 145
6 votes
2 answers
887 views

Singleton design pattern

I am a beginner in PHP-OOP and design patterns. I have got this basic piece of code in Singleton Pattern and I changed it in order to understand its behavior. I can see that it works as expected. ...
DanielTheRocketMan's user avatar
6 votes
3 answers
2k views

Static methods or singleton?

I have class that handles HTTP requests: ...
Dmitry Zaytsev's user avatar
6 votes
4 answers
3k views

Singleton SpinLock: Making Random Thread-Safe

Is this a valid and safe use of .NET's System.Threading.SpinLock? Why am I doing this? Random's public methods are not ...
markmnl's user avatar
  • 173
6 votes
3 answers
41k views

Singleton class and correct way to access it in C++

I'm currently trying to implement the singleton pattern in C++. After reading about it for a while now, it seems like there are a really large amount of different ways to do this. Is the way I ended ...
Eric's user avatar
  • 69
6 votes
2 answers
693 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
2 answers
254 views

Building in the built-in declarations

In order for Rubberduck to be able to "recognize" the built-in VBA functions, procedures and objects, I added yet another constructor parameter to my Declaration ...
Mathieu Guindon's user avatar
6 votes
2 answers
3k views

Simple Java Singleton

Below is a Singleton that I designed as a piece of sample code for a website I'm working on for fun. The goal is to use the Singleton to hold an Earth object since ...
Shijima's user avatar
  • 387
6 votes
1 answer
949 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
6 votes
3 answers
10k views

Several approaches to a logging class

I'm making a personal logger and so far I have come up with 3 solutions. I have them simplified in this example to highlight my dilemma. I want to know the pros and cons of my approaches and ways to ...
PrancingCrabulon's user avatar
6 votes
3 answers
971 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
6 votes
2 answers
2k views

Thread-safe Phoenix Singleton class template with Boost

I've implemented the phoenix-singleton as a class template with boost inspired by Modern C++ Design. The code compiles fine with MSVC 2013 - and it seems to work, but I'm new to multi-threaded ...
PatrickP's user avatar
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
1 answer
1k views

A parametrized Config singleton

Here is an attempt to code a specific kind of Singleton - the one geared for our configuration needs. It needs to be initialized with a configuration location, do not allow copies or other instances, ...
Vlad Didenko's user avatar
6 votes
1 answer
3k views

Searching for songs on an Android application

In my application I need to search for some songs on my external storage card every now and then. On startup I read everything, however when it comes to new activities I thought I could just pass the ...
Davlog's user avatar
  • 677
6 votes
1 answer
173 views

Manage a collection of settings in memory with global scope

I have a WPF app and a bunch of read/write settings that need to be accessible from various view models. a setting can be any type (string, ...
Mr Balanikas's user avatar
6 votes
1 answer
107 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
5 votes
5 answers
980 views

Request for Comments: Singleton pattern implemented in Java

I have several utility classes who should only ever have one instance in memory, such as LogHelper, CacheHelper, etc. One ...
user avatar
5 votes
2 answers
255 views

Address book with singletons

My main goal with this refactor was to change instance methods that should have been class methods in the first place into class methods. My secondary goal was to add singletons to these classes ...
user3344977's user avatar
5 votes
2 answers
91 views

Static variable representing program state accessed with multiple functions

I have this method that performs certain functions based on the state that the algorithm is currently at: ...
C.B.'s user avatar
  • 151
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
5 votes
2 answers
2k views

Android global data

I have a huge list of song objects in my program and I need those objects in almost all activities. Well, at least a part of it up to everything. So I created a class which looks pretty much like ...
Davlog's user avatar
  • 677
5 votes
1 answer
453 views

Two-way data binding

Why reinvent the wheel? I personally like to implement all of my JS from scratch, I try to stay away from using the likes of jQuery or any external sources/libraries/frameworks as much as possible. ...
JO3-W3B-D3V's user avatar
5 votes
3 answers
2k views

Is this good design for custom email template?

I read effective Java by Joshua Bloch. And it said use enums over int constant. So I was thinking instead of using hard coded strings I could also use enums. The forgotpassword.txt and confirmation....
James's user avatar
  • 348
5 votes
2 answers
6k views

Using a singleton class to get and set program wide settings

The following code works and does what I want, but I'd like to confirm I'm using this OOP concept correctly. I'm using the following class to get and set some configuration parameters for my program. ...
user1028270's user avatar
5 votes
2 answers
475 views

Singleton metaclass for each argument set compatible with Pickle

I am creating a metaclass which ensures that instances of an actual class A are kind of singletons. But rather than having only single instance of ...
Tomáš Hons's user avatar
5 votes
3 answers
2k views

Storage class, dependency injection and singletons

In re-writing my PHP framework vervPHP, I've created the following class: ...
EvilChookie's user avatar
5 votes
3 answers
7k views

Design of a remoteControl class for connecting to electronic devices

In a course that I'm taking on learning object oriented programming in java, I have completed an assignment for modeling a home entertainment system and the ability for a user to be able to use a ...
Doublespeed's user avatar
5 votes
1 answer
6k views

Singleton class extending a parent class to utilise shared functionality

I have a singleton class which extends from an abstract java class. Two singleton classes extend from ItemImageThreadManager, the reason for this is to use shared ...
blue-sky's user avatar
  • 557
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
4 votes
4 answers
279 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