Questions tagged [singleton]
A design pattern that ensures that exactly one application-wide instance of a particular class exists. One of the Gang of Four's creational design patterns.
8,451
questions
1
vote
1
answer
10
views
How to create Profile Data singleton class?
I want create singleton class But I don't know how?
Example:
class Singleton {
Singleton singleton = null;
public static Singleton getInstance()
{
if (singleton == null)
...
3
votes
3
answers
113
views
Is Singleton with static unique_ptr a good practice
I work in a project where Singletons are usually implemented like this:
class Singleton
{
public:
static Singleton& get();
virtual ~Singleton() = default;
// Used for unit tests
...
0
votes
0
answers
21
views
how can we use stream and dependency injection with get it package?
get it package only register one time but I need this method in streaming . so what should i do?
I try to call the register method 2 or more times but it maybe not good structure for coding.
final ...
0
votes
0
answers
24
views
Unit test a singleton class with MSTest
I want to test my class EventTextsDB that is a singleton, using MSTest in .NET 6.
I first thought about using AppDomain, but they basically were removed in .NET 5+ (https://learn.microsoft.com/en-us/...
0
votes
0
answers
9
views
How can I maintain shared backend data in Next.js
I'm using Next.js for my website.
I need a set of reference data on the backend of my website that I want to avoid loading from the database on each request. I don't want this data on the client.
How ...
1
vote
1
answer
67
views
C# Singleton pattern using Lazy<T>
I was reading this article by Jon Skeet.
In one the samples, he talks about using Lazy<T> to implement singleton pattern.
This is how the code looks:
public sealed class Singleton
{
private ...
0
votes
1
answer
41
views
Prism register and unregister Singleton
In my App.xaml.cs file I register singleton for my class like below
protected override void RegisterTypes(IContainerRegistry containerRegistry)
{
containerRegistry.RegisterSingleton<...
0
votes
1
answer
41
views
Error implementing singleton in Python with a metaclass
I want to implement singleton with a python metaclass.
I create two threads, instantiate the "File" class and verify that the hashcode of both is the same.
Finally I print the "_file&...
0
votes
1
answer
24
views
Signleton constructor executed multiple times in NextJS 13 middleware
I have a simple singleton named Paths:
export default class Paths {
private static _instance: Paths;
private constructor() {
console.log('paths constructor');
}
public static get ...
0
votes
1
answer
33
views
Is there a way to avoid singleton, yet use same class instance in two different classes at the same time, by dependency injection?
I am building an app, it uses three View Controllers in the same screen at the same time. General structure is something like this:
I want to use dependency injection, I have a main service class. ...
0
votes
2
answers
48
views
Is this a valid Singleton class in a single threaded environment?
The most common example of a Singleton class in a single threaded environment is below:
{
private static Singleton obj;
// private constructor to force use of
// getInstance() to create ...
0
votes
1
answer
23
views
Achieving singleton pattern in python using only __init__ and not using __new__
I am trying to achieve singleton in python. Why is the below code wrong in achieving singleton pattern in Python?
class test:
_instance = []
def __init__(self):
if len(test._instance)!...
0
votes
0
answers
13
views
How to assign value from enum to object in TreeMap?
I need to create a book library that will be a singleton. It contains a map of books. Each book has its own index number and genre (taken from enum). How can I add a method that returns a list of all ...
1
vote
1
answer
44
views
AddSingleton doesn't work in my ASP.NET Core project
I have the problem that I want to add my service CourseService as Singleton and I want to add only once my initial data like this:
context.Courses.Add(new Course { ... });
But it turned out that ...
0
votes
1
answer
24
views
Singleton Class, Annotation
I want to create a lot of Singleton classes in my project,
Can anyone guide me how to create an Annotation that will do my task?
Similar to Lombok do like @Getter, by using this my getters methods are ...
0
votes
1
answer
19
views
Unity: Singleton script needs to be re-attached to gameobject on Editor relaunch to avoid null error?
I'm having a problem with my singleton class scripts I'd like to get your help with.
The script ultimately works, but every time I launch the Unity Editor I need to remove and re-attach the scripts to ...
0
votes
1
answer
34
views
Python - Nested Singleton Classes
Is it possible to nest an arbitrary number Singleton classes within a Singleton class in Python?
There's no problem in changing my approach to solving this issue if a simpler alternative exists. I am ...
0
votes
0
answers
18
views
Field properties in xx.xxx.configuration.ElasticSearchConfiguration required a single bean, but 2 were found:
As part of spring-framework up-gradation from 1.5.6.RELEASE to 2.4.0 i am facing one issue
I am using 2 different ElasticSearch Host so i create 2 different classes one for ESConfigForMasterData and ...
0
votes
1
answer
35
views
Kotlin Ktor Singleton doesn't work in development mode (auto-reload enabled)
I ran into a problem with broken singletons in Kotlin (Ktor Framework).
Problem is particullary Ktor Development mode. Everything is working correctly when it is disabled...
these two calls print out ...
-2
votes
0
answers
42
views
Implement Singleton Design Pattern using Pyton
I have to develop a simple application that if the user press "1", it shows "Miau", if the user press "2", it shows "Auau" and if the user press "history&...
-1
votes
1
answer
44
views
Injecting service in the class / pure functions
I am working on Angular project (Angular 14+) and following atomic design principles (creating minute level components like button, input, toggle etc.).
Most of the views (list, forms, headers) are ...
0
votes
1
answer
40
views
Is connectionString secured from appsettings.json by using singleton pattern?
I am using Singleton pattern to access connection string from appsettings.json because I want to use it from any place. It is a secure approach to access connection string?
the following code used to ...
0
votes
0
answers
20
views
Singleton and inheritance overriding
I m stumbling upon singleton and multiple inheritance.
In the following code if i create slow_grizzly first i got the expected results.
But if i create alaska_bear first the slow_grizlly is ...
0
votes
0
answers
37
views
Alternative to multiple instances of pseudo-singleton
I stumbled upon a pseudo-singleton class that is responsible for housing a few collections. It looks something like this:
public class PseudoSingleton {
private List<Object> collection1;
...
1
vote
1
answer
54
views
Why in Ruby Object's singleton_class isn't Class?
I am a little confused, why is the super class of the singleton class using is_a? method but return false?
I
class Animal
end
obj = Animal.new
singleton = (class<<obj;self;end)
p singleton....
0
votes
1
answer
60
views
How to update variable in Singleton pattern
I have singleton class has lan variable .it is simple like this
class Settings{
// singelton
static let shared = Settings()
var lan:String? = "ar"
private init(){
...
0
votes
1
answer
34
views
Multi Carousel with multi cards Angular
I have several hotel cards that have slider Carousel, but only the first card's carousel works, when I click the other card's indicator , still first card's carousel workingas you can see,i have ngFor ...
0
votes
2
answers
46
views
Should I be using a Class or an Object Literal when implementing a singleton in JS?
I often find myself needing to use a singleton based pattern when creating web apps with typescript and react. Previously I have always implemented these with object literals like so:
interface ...
0
votes
0
answers
47
views
C# single instance class used by multiple EXEs
I have a class that acts as a 'zoneManager', it keeps track of which physical spaces are occupied by moving parts in a machine(robot arms/doors/etc..). Its a separate project that will be added in as ...
2
votes
2
answers
60
views
How and why is the super class of singleton_class of BasicObject is Class class in Ruby?
By definition, a singleton class of an object also inherits the singleton class of the superclass of the object. The BasicObject in ruby doesn't have any superclass as it is the most basic level class
...
2
votes
2
answers
90
views
Check if a parameter of a function is a function
I have a function:
function f(a)
do something that depends on a
end
which behaviour depends on the parameter a. This can be a string, an int or a function itself. For this, I want to check if the ...
0
votes
0
answers
43
views
Is there an interface "ISingleton" I could use for creating a singleton class? [duplicate]
As mentioned in this other question, I'm looking for a way not to create always another instance of a class. I know this can be done, using a singleton.
As I've seen that quite some design patterns ...
5
votes
2
answers
306
views
How to avoid creating the same object over and over again?
I continue working on my TCP sockets, as mentioned this morning.
I have discovered that the amount of sockets keeps growing, every time I send a message over such a socket. This simply means that I am ...
0
votes
1
answer
60
views
How can I track instances of structs in rust?
I have a simple example game entity called a Nation, which is a struct that contains all the data relevant to the player's assets and activities. The player is allowed to change their nation name, ...
0
votes
2
answers
32
views
this singleton has way can be improved?
i was using google's singleton but this must need too many reference.
example, when I have to use another class in my Player class that used singleton, I must be using reference three time. Like this ...
0
votes
0
answers
29
views
Python - argparse - (Singleton) Argument w/ optional parameter [duplicate]
before I code something that argparse can do, I wanted to ask here.
I am using "argparse" to parse the arguments in my python script.
My current approach looks like this, but it does not ...
0
votes
1
answer
44
views
C# MongoDB can we use Collection instance as a singleton?
I have a MongoContext class that is registered as a singleton in DI. It accepts IMongoDatabase instance in the constructor.
Now what is the correct way to get the collection instance?
Use Expression ...
0
votes
1
answer
33
views
Python decorator in class can't be used for methods of other classes
i have written an event system in which functions and metheodes can register as listeners for events. i use a decorator for this. The whole thing worked without problems for functions, but it doesn't ...
0
votes
1
answer
29
views
How to properly use MongoClient to handle two connections to two servers in C#
from time to time, I need to export data from one environment into another environment, each environment is on a different server with different authentication. For instance: I am building a script to ...
-1
votes
2
answers
57
views
Java - Create a text file on the first run [duplicate]
I'm trying to make an application that takes a username and password to store into a text document. However, I can't seem to get the application to create the file on the first run, it only seems to ...
0
votes
1
answer
55
views
C++: Can you have a static and a non-static instance of the same class when using the Singleton design pattern?
I'm trying to understand what's happening under the hood with the following piece of code. The question that I'm having trouble with is: How are static instances of classes handled in c++?
#include &...
0
votes
1
answer
45
views
Flutter Singleton Usage without late
I have learned the how can I use singleton class. After that, I wanted to use for my baseUrl class.
class BaseUrls {
BaseUrls._();
static BaseUrls get instance => BaseUrls._();
final String ...
0
votes
0
answers
52
views
Singleton pattern python [duplicate]
I'm trying to learn how to implement singleton pattern in python. It seems little bit difficult to implement singleton pattern in python.i don't know it's becauses of the nature of the language.
This ...
1
vote
1
answer
63
views
Singleton design pattern. Should its instance be public or private?
Is it necessary for instance of singleton design pattern to be private or public to be considered a singleton design pattern .
for example
class Singleton
{
public static Singleton obj;
is the access ...
0
votes
0
answers
31
views
Laravel create a singleton instance of a class
I want to have a single instance of a Cart object in a Laravel app.
The Cart class need a Customer instance to be initialized.
class Cart {
private Customer $customer;
__construct(Customer $...
0
votes
1
answer
49
views
Create a queue for each singleton Object
I am trying to create singleton instance for each queue. If the instance is already created it should get connected to same instance or else create a new one. I have used the queue to create the ...
1
vote
2
answers
59
views
change property of singleton from another singleton?
I am trying to change the property "MyText" of the class "MainViewModel" from the class "ClassA" under MVVM. I'm not sure if I'm doing this correctly.
Here's my code.
<...
0
votes
1
answer
49
views
Make python logger non-global
I would like to inject a custom logger instance into a use case function:
def api_func(project_dir: str, case_name: str) -> None:
"""This function will be called from web API by ...
-3
votes
1
answer
55
views
Singleton thread safe block multi threading issue
How to get always updated password from the below objects if two users are working parallel? One is updated and same time other user trying to read it?
package com.designpattern.singleton;
public ...
0
votes
1
answer
40
views
Hilt Singleton doesn't seems to work in my Service
I'm facing an issue, like if my repository injected was not a Singleton.
I have a repository (in reality many, but let's make it simple) marked as @Singleton
@Module
@InstallIn(SingletonComponent::...