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
2 votes
1 answer
83 views

Persisting data between HttpRequests in MVC

IMetadata ...
Shelby115's user avatar
  • 1,961
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 ...
Mathieu Guindon's user avatar
2 votes
1 answer
3k views

Dependency injection thread safety

Is it OK to mix different lifetime of ISerializer and ICacheProvider in this case? I am using Unity DI. In this case, whenever <...
crseuser's user avatar
0 votes
1 answer
1k views

Ruby singleton for logging

I currently have a logging setup that looks like this: ...
Anthony's user avatar
  • 439
1 vote
1 answer
18k views

A Singleton Base and derived class

I have a base and derived class since I need to have different kinds of concrete classes. I cannot use the getInstance() function in the base class since I cannot ...
Eternal Learner's user avatar
3 votes
1 answer
620 views

Reducing lock contention for a caching utility, or make it totally lockless but threadsafe

My Java program is a module called configProxy which fetches configuration entries from a remote node (say from a Redis instance). When the caller calls the ...
Mopparthy Ravindranath's user avatar
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
4 votes
1 answer
204 views

Correct usage for singleton and reusability?

I've made the following DataSetReader, DS Attribute, and DataSetManager. My main concerns ...
Shelby115's user avatar
  • 1,961
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
0 votes
1 answer
283 views

PHP framework building: initializer and object loader classes

I am building a PHP framework and would like to get some feedback on a few different sections of the project. I consider myself still a neophyte in PHP so I would like to ask if I'm going about ...
andrewnite's user avatar
1 vote
1 answer
57 views

Managing a collection of object instances and getting them back out with instance manager / service locator

I'm creating an event loop for an asynchronous event execution library for PHP in PHP. Note that this is for a library and not an application. My event loop is just that -- a simple event loop that ...
user avatar
3 votes
1 answer
170 views

Execute startup method asynchronously

The goal is to call startManager on application start so that the manager is initialized in background without blocking application main thread. Is it a safe ...
Michele Mariotti's user avatar
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
3 votes
1 answer
463 views

Blocking builder with singleton

I am trying to implement a singleton builder that would be shared across multiple builder threads for Coordinate objects. Here is the simplified target class ...
karakays's user avatar
  • 131
3 votes
1 answer
1k views

Ensure only a single instance of static template class member

I am interacting with a C library, whose documentation states that certain functions are not thread safe. Since its functions deal heterogeneously with multiple types, I have written a template ...
wakjah's user avatar
  • 133
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
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
2 votes
2 answers
3k views

Proper way to wait end of a service and singleton design

My code works fine and I would like to know if I chose the best strategy to implement my code, or if It can written better (more fast, more clear, best use of design pattern, etc). In my android ...
Accollativo's user avatar
3 votes
1 answer
431 views

Lazy singleton with BackboneJS

I am trying to have a Backbone.Model which is a singleton and I wish to instantiate it lazily. I only need to support modern browsers so please do not worry about the use of ES5 getter. Is this the ...
Sean Anderson's user avatar
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
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
3 votes
2 answers
6k views

Correct way to implement a Singleton field

I have a helper class that does HTTP operations on Android. Every method uses a client field. I think it's a good idea to make this client a singleton since every method in the class uses it. The ...
VM4's user avatar
  • 131
4 votes
2 answers
2k views

Necessary singleton in DAO

I am using a singleton in my DAO. Would I be better off with static methods? Also, should I be worried about synchronization of the singleton and my data structures?...
user3590149's user avatar
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 ...
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
2 votes
2 answers
3k views

PHP Trait Singleton

An implementation of the singleton pattern in PHP using a trait (added in 5.4). Is there anything missing, any ways to create a second copy of the class? ...
Spencer's user avatar
  • 217
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
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
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
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, ...
Mr Balanikas's user avatar
0 votes
1 answer
166 views

Singleton-like pattern for a project [closed]

I'm changing the inner implementation of a project. In order to do so, I've created an interface that will be implemented in 2 different ways. One of this ways is by class ...
sivan shani's user avatar
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
4 votes
1 answer
666 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 ...
Logan's user avatar
  • 303
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. ...
DanielTheRocketMan's user avatar
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, ...
Vlad Didenko's user avatar
4 votes
1 answer
3k views

ResourceManager Singleton

As I understand creating lots of resource managers in C# may be a bad idea so I thought that the best thing to do would be to create a singleton for this. However, I'm not 100% if this is a good idea,...
John's user avatar
  • 143
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
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
3 votes
1 answer
132 views

How do I avoid explicit type switching when operation based on state?

I've encountered a problem that I have to solve using dynamic_cast to invoke different functions, depending on the type of class State family. I was re-factoring a ...
user3136552's user avatar
1 vote
1 answer
3k views

OOP MySQLi singleton

I'm starting with object oriented programming, but I know PHP procedural style "pretty well" I'd say. I'm working on a ticket support site for this website. This ...
Leonardo Chaia'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
0 votes
1 answer
603 views

Creating a singleton container class for intrer-class messaging

My question is for validation of my code (any loop holes / bugs) and guidance the best methodology to implement for my requirement. I am developing a Python application that will have many classes. ...
user32034's user avatar
2 votes
2 answers
966 views

I need code review on using singleton to access local database

As I get to some performance issues in my app and find that I use database access in a bad way. So I decided to move to singleton pattern. I need someone to review this code and confirm me that I made ...
1110's user avatar
  • 217
2 votes
1 answer
239 views

Singleton has logic and state, and logic has state

------edit--------- note: this is for a MUD client, so the commands are issued to the MUD game server -----end edit-------- For an overview, here's the structure of the project: ...
Thufir's user avatar
  • 379
3 votes
1 answer
5k views

PDO class for multiple databases

I have a PDO class below: ...
XTRUST.ORG's user avatar
3 votes
3 answers
4k views

Is this code thread-safe - Singleton Implementation using Concurrent Dictionary

...
rtindru's user avatar
  • 153
18 votes
3 answers
44k views

Singleton using enum

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

Global data singleton

I need a global data in my application. My application uses several threads to access and add items to my global variable. The global variable is a ...
Luke101's user avatar
  • 243
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. ...
rycle's user avatar
  • 145
3 votes
2 answers
8k views

Singleton interface in Java

I've created a singleton by means of an interface. I don't want to make a thing with getInstance() etc because I don't care about inheritance, and it's redundant ...
Dog's user avatar
  • 131