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
4 votes
1 answer
144 views

Alpha finance trader singleton

I used to write singleton in Python, which actually Alpha only need to initialize once and the trading bot will run forever. For example in main.py: ...
2 votes
2 answers
817 views

Objective-C singleton implementation

Is this the right way to do a singleton in Objective-C (coming from an Android background, with a little understanding of threading)? ...
-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: ...
0 votes
1 answer
124 views

Singleton generic implementation of simple LinkedList

I would like to ask if the implementation is ok, and what I can improve. Although I'm not sure if the Singleton is working properly. ...
0 votes
1 answer
99 views

Rust: Unsecure Static Once

I am basically a beginner, so I Wrote some code that implements a zero cost singleton, through it will crash your program if you have a MMU. ...
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 ...
1 vote
2 answers
381 views

C++ Settings Manager

I'm starting a new project, and it needs to be set up using YAML files. To handle all the settings, I've created a singleton class, I'd like you guys to review. Here is the header: ...
2 votes
1 answer
4k views

Polly AddPolicyRegistry() with factory and registered dependency injection service instances?

The Microsoft.Extensions method for Polly, to use policies via dependency injection, serviceCollection.AddPolicyRegistry() only allows to add already created ...
2 votes
1 answer
157 views

Singleton in Ruby without using Singleton module or class variable

When I need a singleton class in my Ruby code (for example, single logger for multiple classes) I usually use code like this: ...
1 vote
2 answers
4k views

Java MongoDB client as a singleton

Is below code a valid Singleton design pattern for Mongodb in Java? Please suggest any improvements. ...
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 ...
1 vote
3 answers
322 views

Singleton MongoClient instance provider

A Java service that connects to MongoDB in production, but opened connection count is too much. It affects on MongoDb performance, so I have come up with a solution that provides only one ...
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 ...
0 votes
1 answer
732 views

Generic Meyers Singleton implementation in C++

Is this a correct implementation of a generic Meyers Singleton in C++17? Any tips appreciated. Singleton.h ...
3 votes
2 answers
5k views

ASP.NET Core singleton with a thread-safe property that can be changed

Thanks to ASP.NET Core dependency injection, there's no need to follow the Singleton pattern; any class will do. What I want to do is to have a class that will store a value that'll be used by another ...
1 vote
2 answers
1k views

Magic Static in Singleton Template

I recently was reading an article labeled "Modern C++ Singleton Template". The proposed solution used C++11 feature of Magic Statics (N2660). I wanted to adapt it for an own project in "...
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 ...
0 votes
1 answer
91 views

Inheritance with Singletons [closed]

The problem / background I have a project, BusinessProject, that currently stores some static data in memory using the Singleton pattern - we'll call this class ...
-1 votes
1 answer
1k views

Fast efficient C++ Singleton template with proper constructor and destruction order

I wrote a Singleton template, with examples, google tests and README https://github.com/erez-strauss/init_singleton/blob/master/singleton.h The usage can be as simple as: ...
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 ...
2 votes
0 answers
149 views

SharedPreferences singleton wrapper

I was wondering wether my implementation of the PreferenceService below is thread safe or could cause memory leaks. Android Studio gives me the warning "Do not place Android context classes in ...
1 vote
0 answers
35 views

Usage of global variable for dynamic value that must be declared compile time [closed]

I've been trying to make a work-around for global usage; however, I can't seem to find an alternative. I'd like to create a C++ suffix function which converts pixels to millimeters. A custom gui ...
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: ...
1 vote
1 answer
2k views

Destroy Singleton pattern in kotlin

I am practicing Singleton pattern in kotlin. It is a quiz app which has question tile with four options and one correctIndex. McqOldActivity.classs ...
1 vote
1 answer
84 views

Using enum for singleton implementations to choose way of doing something

I have this service that reads emails from a mailbox as they come and I want to extract its contents in a map structure. For now I am interested only in the actual email (no images or attachments). I ...
2 votes
0 answers
2k views

Singleton pattern for Redis connection

Recently I have learned about singleton design pattern. I have tried to implement Redis connection following singleton design pattern. conn/redis.go ...
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 ...
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 ...
2 votes
1 answer
374 views

Proxy to hide singleton implementation [closed]

I have a resource (e.g., current program configuration) that I want to handle in a single class instance, but want to hide the fact that there is, in effect, a singleton class by using a proxy class. ...
1 vote
1 answer
1k views

Singleton class to contain a DocumentClient instance, that can be overridden for testing

The DocumentClient is an object that accesses CosmosDb, but Microsoft recommends its kept as a ...
2 votes
0 answers
67 views

Generic DialogueBox class with different parameters

I have implemented a DialogueBox class which can be static, singleton, service locator, etc. I have written it as a singleton pattern and implemented it in Unity3d engine. It is a part of a game. I ...
4 votes
3 answers
5k views

Simple Publisher Subscriber in Java

I am new to Java and had some difficulty creating this simple Publisher Subscriber class. It finally seems to be working and I wanted to check if I have been following best practices and see if there ...
0 votes
2 answers
890 views

Storing singleton instances in a generic utils class

In one of my java projects I use singletons a lot and I was looking for a way to reduce repeating code. I made a SingletonUtils class that accepts a class and ...
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? ...
1 vote
2 answers
75 views

Singleton rewrite based on old one

I inhereted an implementation of a singleton template. Althoug I read that singletons are bad, my program uses singletons throughout and I can not get rid of them at the moment. This is the ...
0 votes
1 answer
478 views

PHP OOP registration with User class and singleton Database class [closed]

I created registration system with PHP. I have User class that will handle all the input and insert the user input into database, and i created Database class with Singleton pattern that will connect ...
3 votes
1 answer
4k views

Static class for constants with reference to java resources

I want to store the colors I use in the colors.xml file. In addition to using them in an xml-layout, I also want to use these colors in my Java code, when drawing ...
3 votes
1 answer
2k views

Implementation of an OkHttp singleton for Android

I'm new to Android programming. I am going to make lots of HTTP requests: 500, 1k, 2k, 10k. I have tried to create a singleton class for OkHttp. I wrote this: ...
0 votes
2 answers
241 views

Create another Singleton object from a Singleton class

I want to create a singleton object of a third party caching library (ThirdPartyKVStore). But I don't want to reference this class all over the place from where I ...
0 votes
3 answers
6k views

Java Singleton getter/setter

I've created working setters/getters for an application, that will use a few different classes. This classes will use one class, that will store all data. I know, when I will use a standard ...
0 votes
2 answers
255 views

Optimizing UITableViewCell with singleton property

In UITableViewCell class, I have two properties: UINavigationController ...
3 votes
1 answer
243 views

Multilingual app without localizing

I'm making an iOS app which supports multiple languages but not in the built-in localized way. In iOS, for localization to work, the user has to change the device's language. But in the app I'm ...
1 vote
0 answers
562 views

Creating a logger in the top level of my react app and then using it in other components as I need it

...
4 votes
1 answer
1k views

Multi-threaded Time Scheduler without polling

This is a quick shot at designing a time scheduler without polling. It is able to handle inputs which are to be executed earlier than already on the schedule. I want to improve it further, especially ...
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 ...
2 votes
0 answers
48 views

Creating an object handler that loads objects in an array in PHP

I am creating tooltips that I will use throughout my site. Each individual tooltip is created as an object, all of these tooltips need to be ingested with these rules: I should be able to hold an ...
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. ...
1 vote
1 answer
277 views

shared_instance implementation using C++ and RAII

I created a little singleton-like class that gets deleted when there is no reference to it anymore (so its lifetime shall be somewhat predictable). I aimed to make ...
3 votes
1 answer
3k views

Attempting to make my properties an enum based singleton

I was attempting to implement my first singleton using an enum, was a bit of a struggle for me and I'm not sure if its right and more importantly thread safe? Property provider enum: ...
2 votes
1 answer
478 views

Fed Singleton struct class from plist

I have a plist file which has URLs in it and struct class is feeding from this plist. This struct class has static variable which I use to reach URLs in every part of the app. My question is that I'm ...