Questions tagged [singleton]
The singleton is a design pattern to ensure that exactly one application-wide instance of a particular class exists.
179
questions
67
votes
9
answers
29k
views
Implementing a Singleton pattern in C#
Is this the best way to implement this pattern in C#?
...
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 ...
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 ...
31
votes
1
answer
29k
views
Single-instance WPF application
This is the code I implemented so far to create a single instance WPF application:
...
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?
...
20
votes
3
answers
61k
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 ...
18
votes
3
answers
44k
views
Singleton using enum
Is this the correct implementation of a singleton using enum?
...
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:
...
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
...
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 ...
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 ...
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 ...
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 ...
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
...
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:
...
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 ...
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, ...
7
votes
5
answers
7k
views
A generic singleton
What do you think about this for a generic singleton?
...
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 ...
7
votes
1
answer
501
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
2
answers
874
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
3
answers
2k
views
Static methods or singleton?
I have class that handles HTTP requests:
...
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 ...
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 ...
6
votes
2
answers
252
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
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 ...
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 ...
6
votes
3
answers
9k
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 ...
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:
...
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 ...
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 ...
6
votes
1
answer
987
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
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 ...
6
votes
1
answer
169
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
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 ...
5
votes
5
answers
979
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 ...
5
votes
2
answers
253
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
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:
...
5
votes
3
answers
37k
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 ...
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 ...
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 ...
5
votes
1
answer
442
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. ...
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....
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. ...
5
votes
2
answers
429
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 ...
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:
...
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 ...
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 ...
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 ...
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 ...