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
9answers
28k views

Implementing a Singleton pattern in C#

Is this the best way to implement this pattern in C#? ...
40
votes
7answers
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 ...
38
votes
4answers
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 ...
29
votes
1answer
25k views

Single-instance WPF application

This is the code I implemented so far to create a single instance WPF application: ...
22
votes
2answers
24k views

Thread-safe Singleton class

I have created this class to simulate a thread-safe Singleton. Have I missed anything? ...
17
votes
3answers
43k views

Singleton using enum

Is this the correct implementation of a singleton using enum? ...
16
votes
2answers
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: ...
12
votes
5answers
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 ...
12
votes
1answer
130 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 ...
12
votes
2answers
3k 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 ...
11
votes
3answers
22k 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 ...
9
votes
4answers
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 ...
9
votes
3answers
1k views

Storing a Ruby app's settings

I have used code which looks a little like this in an app I'm working on: ...
9
votes
4answers
21k 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 ...
9
votes
1answer
9k 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 ...
8
votes
3answers
966 views

Singleton Database wrapper

Before everybody yells at me about why I wrote a PDO wrapper class, it's to avoid writing try/catch, ...
8
votes
1answer
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 ...
7
votes
5answers
5k views

A generic singleton

What do you think about this for a generic singleton? ...
7
votes
2answers
3k 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 ...
7
votes
1answer
462 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. ...
6
votes
2answers
745 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. ...
6
votes
3answers
2k views

Static methods or singleton?

I have class that handles HTTP requests: ...
6
votes
4answers
2k 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 ...
6
votes
2answers
571 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 ...
6
votes
2answers
234 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 ...
6
votes
2answers
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 ...
6
votes
1answer
753 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 ...
6
votes
3answers
823 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: ...
6
votes
2answers
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 ...
6
votes
1answer
134 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 ...
6
votes
1answer
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 ...
6
votes
1answer
496 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, ...
6
votes
1answer
156 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, ...
6
votes
1answer
98 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 ...
5
votes
2answers
219 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 ...
5
votes
2answers
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 ...
5
votes
3answers
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....
5
votes
3answers
3k 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 ...
5
votes
3answers
2k views

Storage class, dependency injection and singletons

In re-writing my PHP framework vervPHP, I've created the following class: ...
5
votes
3answers
5k 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 ...
5
votes
1answer
5k 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 ...
5
votes
1answer
794 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 ...
4
votes
5answers
821 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 instance to rule them all. I've never implemented Singleton in Java before, ...
4
votes
4answers
246 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 ...
4
votes
4answers
4k views

Singleton Decorator

I've written a class that makes writing singletons easy. The class is meant to be used as a decorator (example below). I'd like to see your thoughts about the code, the documentation, and everything ...
4
votes
3answers
416 views

ObjectManager - Single Instance Object managerment

I have designed an objectservice for a single threaded game rpg server. It's purpose is to store the entire set of objects in a single instance shared by all connected clients. it's purpose it follows ...
4
votes
2answers
83 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: ...
4
votes
1answer
646 views

Singleton With Sub-Singletons

I've found myself wanting an easy way to implement chat into various apps, so I developed a set of classes for the firebase.com backend that make it easy for me to quickly set up the nuts and bolts of ...
4
votes
2answers
5k 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. ...
4
votes
1answer
2k views

Is this a good implementation of a thread-safe singleton using the observer pattern?

I need a singleton that can safely operate in a multi-thread environment. Threading and concurrency is new to me, so I'm not sure if this implementation holds. Take a look: ...