How do I implement convenient logging without a Singleton?

First: the use of std::unique_ptr is unnecessary: void Log::LogMsg(std::string const& s) { static Log L; L.log(s); } Produces exactly the same lazy initialization and cleanup semantics without introducing all the syntax noise (and redundant test). Now that is out of the way… Your class is extremely simple. You might want to build a slightly more … Read more

ASP .NET Singleton

Static members have a scope of the current worker process only, so it has nothing to do with users, because other requests aren’t necessarily handled by the same worker process. In order to share data with a specific user and across requests, use HttpContext.Current.Session. In order to share data within a specific request, use HttpContext.Current.Items. … Read more

How to use scala.None from Java code [duplicate]

The scala.None$.MODULE$ thing doesn’t always typecheck, for example this doesn’t compile: scala.Option<String> x = scala.None$.MODULE$; because javac doesn’t know about Scala’s declaration-site variance, so you get: J.java:3: incompatible types found : scala.None$ required: scala.Option<java.lang.String> scala.Option<String> x = scala.None$.MODULE$ ; This does compile, though: scala.Option<String> x = scala.Option.apply(null); so that’s a different way to get a … Read more

“Singleton” factories, ok or bad?

It really depends on what you’re doing and the scope of your application. If it’s just a fairly small app and it’s never going to grow beyond this, then your current approach may well be fine. There is no universal “best” practice for these things. While I wouldn’t recommend using singletons for anything other than … Read more

Difference between static function and singleton class in swift [closed]

Sure this sounds confusing and can be debated. However, from the best practices i can put some suggestions. Singleton is usually used to create a resource intensive and one timer initialisation for instance: a database connector, login handler and such. Utility class are classes that only have static functions and variables. It should not deal … Read more

Kotlin singleton application class

You can do the same thing you would do in Java, i.e. put the Application instance in a static field. Kotlin doesn’t have static fields, but properties in objects are statically accessible. class MyApp: Application() { override fun onCreate() { super.onCreate() instance = this } companion object { lateinit var instance: MyApp private set } … Read more

Java double checked locking

Double check locking is broken. Since initialized is a primitive, it may not require it to be volatile to work, however nothing prevents initialized being seen as true to the non-syncronized code before instance is initialized. EDIT: To clarify the above answer, the original question asked about using a boolean to control the double check … Read more

When would the garbage collector erase an instance of an object that uses Singleton pattern?

There’s a static reference to a singleton, so it won’t be eligible for garbage collection until the classloader is eligible for garbage collection. You can’t force any object to be garbage collected; you can request that the garbage collector runs with System.gc() but it’s only a request. If you really want to make a “singleton” … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)