8,749 questions
Best practices
2
votes
4
replies
107
views
When is it OK to use a singleton?
Follow-up on this question: Singletons: good design or a crutch?
It is true, that using singletons can make testing harder, and bugs can hide behind them. GoF defines singleton to use when:
Global ...
3
votes
1
answer
85
views
Cannot instantiate singleton class implemented by metaclass with default value
I want to instantiate class with default value.
Class have required parameters.
Class must be singleton (can be implemented with smt other than metaclass).
Data must be updated if try to instantiate ...
0
votes
0
answers
45
views
Singleton pattern in Rust [duplicate]
Goal
Implement a Singleton Design Pattern for a struct, without multiprocessing considerations.
Minimal example
Here is the base of the code, without the singleton logic.
struct ...
0
votes
0
answers
48
views
why does preferencesDataStore() return the same instance? (why a class is a singleton? no need for dagger-hilt?)
This is a Kotlin question, although it originated from Android Jetpack Compose.
I defined the Context.dataStore extension property in the recommended way:
val Context.dataStore: DataStore<...
0
votes
0
answers
44
views
Messy call graphs for singleton classes
I'm trying to browse the C++ code for an embedded project using Doxygen 1.8.13.
I find the call/caller graphs very useful for this, but the project includes several singleton classes. Each of these ...
2
votes
2
answers
79
views
Forbid multiple service implementations in .NET Generic Host service collection
For .NET Generic Host it is possible to register multiple implementations of same service interface. When materializing that service the last added is obtained (that is correct even for multiple ...
0
votes
1
answer
77
views
Perform an action when all registered services in .NET Generic Host are disposed
I need to perform some action (i. e., log succesfull shutdown) when all registered services in .NET Generic Host are disposed. I hoped IHostApplicationLifetime.ApplicationStopped could help me but it ...
2
votes
1
answer
112
views
Singleton Dependencies in Background Task Channel / Hosted Background Service
I am working on creating some ASP.NET Core 8.0 Web API endpoints (in C#). There will be over 100 endpoints across 40-50 repositories that are all a part of the same platform ecosystem; it is not an ...
-1
votes
1
answer
101
views
How to handle static variable sharing in dynamic linking of libraries?
I have a requirement in which I have to use a singleton class, acting as a database to store some values. I have also declared a run function in the cpp file of this singleton class in order to change ...
0
votes
2
answers
40
views
Python subclass constructor calls metaclass with *args and **kwargs
I am trying to instantiate a singleton with constructor argument. However, passing the input arguments to the metaclass hits an error.
from typing_extensions import List, TypedDict, Optional, Any
...
0
votes
1
answer
65
views
Error in F# 9.0 on singleton pattern: The static initialization of a file or type resulted in static data being accessed recursively
In F# FSharp.Core(8.0.400) and Windows .NetCore 3.1, the following singleton pattern worked flawlessly:
[<AutoOpen>]
module AppointmentEventManager
open Stargate.XI.Client.Domain.Models
type ...
0
votes
2
answers
110
views
Singleton for UIViewControler in Swift disallowed by compiler?
I have a UIViewController class with a variable shared that allows other classes to reach it as a singleton. However, the compiler (with Main Thread Checker enabled in Scheme Settings) is flagging the ...
1
vote
2
answers
160
views
setting thread safe dictionary of runtime vars using contextvars
Basic process explanation:
A process has some initialization parameters that have to be set dynamically at runtime. For example data for a certain amount of dates (final_lookback) have to be pulled ...
0
votes
0
answers
33
views
WebSocket Connection Causes Delay on App Revisit in React Native
Problem Description:
I am using a singleton SocketConnect class to manage WebSocket connections in my React Native application. The connection works fine initially, but when I leave the app and return ...
-2
votes
1
answer
61
views
Why does the singleton class in iOS, when combined with multiple dynamic frameworks, result in multiple instances?
The main project ModuleDemo depends on ModuleA, ModuleB, and ModuleC. The description of the entire project's Podfile is roughly as follows:
platform :ios, '13.0'
use_frameworks! :linkage => :...
0
votes
0
answers
79
views
Kotlin "Cannot set property because no setter, no wither and it’s not part of the persistence constructor" Error
I have object A like this:
sealed class B {
private val d: String = this::class.simpleName!!
data object C: B()
}
data class A(
val b: B,
)
And I have faced this error when I want to get object A ...
2
votes
0
answers
149
views
NSCache singleton - concurrency safe (Swift 6)
I would like to make sure I understand this correctly. I created a singleton class managing cache of NumberFormatters and compiler complains about shared property not being concurrency-safe. Reasons ...
0
votes
1
answer
33
views
Python Singleton - Avoid "None" reference
I have created a class that I"m attempting to implement a Singleton pattern on. When __new__ for the class is fired off, I check to see if a class-wide list of instances has a length > 0 or ...
-2
votes
2
answers
100
views
Correct singleton pattern implementation? [duplicate]
I have the following singleton implementation based on a video https://youtu.be/Q6HJpgdkAK8?si=k-9ksjHirLHq5Ne2 Some features of my own implementation are different, but i want to know whether my ...
1
vote
0
answers
47
views
Implementation of sinleton class, private constructor, still initiallizing, do I need to access Singleton from another class to make it work? [duplicate]
how am I able to create an instance of Singleton s3 even when the constructor is private, tried in local env and online compiler as well, it is running
public class Singleton {
// Step 1: ...
-3
votes
1
answer
93
views
I have a question about using some shared resources as static variables [closed]
I'm a student who is studying game engine structure.
I've been managing shared resources through the Singleton Patterns Manager class while creating game engines. However, as the code got longer, the ...
2
votes
1
answer
125
views
How to implement singleton object in C?
I have a requirement for an OBJECT to be used by other objects. The requirement is that the OBJECT not require initialization by any external call. So I began with something like so...
// .h
struct ...
0
votes
1
answer
107
views
Singleton class with std map of unique_ptr is throwing error on compilation
The following code is failing to compile and i am not able to figure out why!
/* --------------------------------------------------------------- */
/* Includes */
#include <iostream>
#include &...
0
votes
1
answer
53
views
Is it possible to have python workers use different global context?
We have a singleton object we use for our program that refers to an ugly god-object that I wish would die - we inherited the code from a team that had already implemented it this way. Unfortunately ...
1
vote
1
answer
517
views
Unity - Why is my Singleton throwing a NullReferenceException when I re-load the scene?
I am facing an issue with a NullReferenceException in my code. I've created a LevelManager script to manage the different scenes of my game. Inside this file ScoreKeeper object is called. This ...