singleton
Python: thinking of a module and its variables as a singleton — Clean approach?
A common alternative to using a module as a singleton is Alex Martelli’s Borg pattern: class Borg: __shared_state = {} def __init__(self): self.__dict__ = self.__shared_state # and whatever else you want in your class — that’s all! There can be multiple instances of this class, but they all share the same state.
C++, static vs. namespace vs. singleton
As noted, using global variables is generally bad engineering practice, unless absolutely needed of course (mapping hardware for example, but that doesn’t happen THAT often). Stashing everything in a class is something you would do in a Java-like language, but in C++ you don’t have to, and in fact using namespaces here is a superior … Read more
How to constrain a table to contain a single row?
You make sure one of the columns can only contain one value, and then make that the primary key (or apply a uniqueness constraint). CREATE TABLE T1( Lock char(1) not null, /* Other columns */, constraint PK_T1 PRIMARY KEY (Lock), constraint CK_T1_Locked CHECK (Lock=’X’) ) I have a number of these tables in various databases, … Read more
How do you implement the Singleton design pattern?
In 2008 I provided a C++98 implementation of the Singleton design pattern that is lazy-evaluated, guaranteed-destruction, not-technically-thread-safe: Can any one provide me a sample of Singleton in c++? Here is an updated C++11 implementation of the Singleton design pattern that is lazy-evaluated, correctly-destroyed, and thread-safe. class S { public: static S& getInstance() { static S … Read more
Singleton lazy vs eager instantiation
With lazy initialization you crate instance only when its needed and not when the class is loaded. So you escape the unnecessary object creation. That being said there are other things to consider too. In lazy initialization you give a public API to get the instance. In multi-threaded environment it poses challenges to avoid unnecessary … Read more
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