Stack Exchange Network

Stack Exchange network consists of 175 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [singleton]

The singleton is a design pattern to ensure that exactly one application-wide instance of a particular class exists.

0
votes
0answers
40 views

Destroy Singleton patter 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 ...
2
votes
1answer
54 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. ...
0
votes
1answer
86 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
0answers
63 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 ...
1
vote
3answers
140 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
2answers
103 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 ...
1
vote
2answers
45 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 ...
-1
votes
1answer
128 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 ...
2
votes
3answers
5k 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 ...
2
votes
1answer
501 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: ...
3
votes
1answer
404 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 ...
5
votes
3answers
1k 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
0answers
34 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 ...
3
votes
1answer
166 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 ...
3
votes
1answer
166 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. ...
3
votes
1answer
188 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: ...
3
votes
1answer
158 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 ...
2
votes
1answer
2k 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 ...
2
votes
1answer
349 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 ...
4
votes
3answers
391 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 ...
0
votes
2answers
100 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 ...
1
vote
1answer
741 views

HttpClient wrapper for simple calls with optional cert

Trying to ensure each controller contains it's own httpclient. The super user can change which endpoint is called from the site and some of those endpoints do not require a certificate, but some ...
0
votes
3answers
1k 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
2answers
135 views

Optimizing UITableViewCell with singleton property

In UITableViewCell class, I have two properties: UINavigationController ...
1
vote
1answer
776 views

Singleton with a volatile and synchronized instance

I created a singleton to have a unique instance of ExecutorService in my application. I whould like to know which is the better of the two classes below. First one : ...
0
votes
1answer
1k views

Java MongoDB client as a singleton

Is below code a valid Singleton design pattern for Mongodb in Java? Please suggest any improvements. ...
1
vote
0answers
184 views

Singleton lock for a queue

This is a singleton class which can be accessed anywhere including the shouldLock variable. I want to make sure that the variable is thread safe. Basically the <...
0
votes
1answer
276 views

Singleton design pattern study

I am learning design patterns and the singleton is one of them. What are your opinions on the code? Please feel free to share. I know singletons are not popular because of complications in testing. I ...
6
votes
2answers
14k 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 ...
2
votes
0answers
143 views

Comments on singleton Swift status bar alert

I was trying out some projects like a status bar alert. It made most sense for this status bar to be a singleton: there is only one alert visible at a time. ...
2
votes
1answer
416 views

ASP.NET Web Forms Singleton

Is this way acceptable to do a C# singleton for use in ASP.NET webforms legacy app: ...
-1
votes
1answer
131 views

PHP Simple Admin

I need to create a small app that can add options, save them into a database and retrieve info, which are painted in Panel and an Editor, and I've decided to do it manually, and trying to figure out ...
3
votes
2answers
6k views

Singleton implementation of a database connection

I have implemented as follows, a class applying singleton pattern to get a global single access to database. I intend to provide a thread-safe implementation. ...
2
votes
0answers
40 views

Classes to help transform “How hard is it to …?” questions into replies like “It's really hard to …”

I've written a Twitter bot, @answering_yelp, which responds to another Twitter bot, @hard_to_yelp. They scrape yelp reviews for sentences which start "How hard is it to..." and my bot answer with some ...
0
votes
3answers
1k views

C++ singleton using templates

Is this a good Singleton implementation? Is there anything I should be aware of? If so, how can it be improved? ...
5
votes
1answer
733 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 ...
1
vote
1answer
220 views

An improved single instancing library

I previously posted a question: Single instancing class and received some good feedback on it. I have implemented most of the changes suggested by @EBrown. I also implemented the suggestion by @...
0
votes
1answer
64 views

PHP Singletons Wrapper (Dynamic Admin) [closed]

I need to create an administration board that allows devs to create or modify options and display them in a panel. The Admin needs to be the same on each call made to it. I've chosen to make all ...
3
votes
1answer
1k views

Log4j Singleton

I'm trying to design and develop a singleton class that will handle resolution and configuration of log4j (not log4j2). The basic idea is to have a single class, reusable in any application, that ...
0
votes
1answer
213 views

Monitoring errors with singleton

I need to monitor my functions for "errors" and want to print the warnings at the end of the functions. I thought using singleton class could be a good idea in here (code inspired by this example): <...
1
vote
1answer
97 views

Is this a good and simple example for Singleton Pattern [closed]

This is a scenario where we keep a single instance(as in Singleton Pattern) to get the configuration of an application. Below is the main class and i set values to the configuarion values here and ...
3
votes
2answers
521 views

Caching all the prepared statements in thread safe way

I have a below Singleton class where in my getStatement method, I populate a CHM by doing if check. ...
3
votes
2answers
273 views

Singleton Pattern + unavoidable Public Constructor

We have an object that needs to be converted to a different type before storing it in database. For that purpose, the library provides an annotation over the field that specifies the appropriate class[...
11
votes
2answers
2k 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 ...
0
votes
2answers
2k 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 ...
4
votes
1answer
191 views

JSON loader with event bus, cache, and session

I have an Android application which needs to retrieve JSON files from a server and then display the information in my app. To accomplish this I have the following architecture: The main activity adds ...
3
votes
3answers
2k views

Base class for singletons

Even though singletons are bad practice and often unnecessary, I still see many developers using this pattern over and over. Since implementation of this pattern usually requires some common code, I ...
1
vote
1answer
220 views

Singleton Provider

I've taken over a code base that is still very much in it's infancy, and stumbled across this class. I'm just wondering if it's actually worth it to have a ...
1
vote
1answer
54 views

How can I extract these similar codes into only one method? [closed]

I have implemented three ways of Singleton idiom in Java, and now I want to write some test code to see there performance. Here is my code: ...