singleton

Instantiating and using an enum singleton

流过昼夜 提交于 2019-12-22 11:29:14
问题 Say you have an enum singleton: public enum Elvis { INSTANCE; private int age; private Elvis() { age = 42; } public int getAge() { return age; } public void leaveTheBuilding() { System.out.println("I'm outta here."); } } Question: how do you then use it? Is it like this: int a = Elvis.INSTANCE.getAge(); Elvis.INSTANCE.leaveTheBuilding(); // and so on, using Elvis.INSTANCE or is it preferable to instantiate it "explicitly" and then use that instance, like so: Elvis elvis = Elvis.INSTANCE; int

Implementing the Singleton pattern for accessing an object anywhere

ぐ巨炮叔叔 提交于 2019-12-22 11:28:36
问题 Currently, I have a class whose constructor takes a username, password, and Context. I would like to be able to access this object from anywhere, so I was thinking of implementing a singleton pattern. The current constructor uses the credentials passed in to authenticate future api calls through that class. If I were to implement the singleton pattern, my first thought would to be have the getInstace() method take a username, password, etc.., but it seems wrong to have to pass that info

Canonical Way to Ensure Only One Instance of a Service Is Running / Starting / Stopping in Clojure?

点点圈 提交于 2019-12-22 10:53:06
问题 I'm writing a stateful server in Clojure backed by Neo4j that can serve socket requests, like HTTP. Which means, of course, that I need to be able to start and stop socket servers from within this server. Design-wise, I would want to be able to declare a "service" within this server and start and stop it. What I'm trying to wrap my mind around in Clojure is how to ensure that starting and stopping these services is thread-safe. This server I'm writing will have NREPL embedded inside it and

Should this “singleton” be thread-safe in an ASP.NET app?

扶醉桌前 提交于 2019-12-22 10:38:23
问题 Note that "singleton" used in slightly uncommon sense - "object visible as single instance like HttpContext.Current" unlike normal "object with one shared instance". I make use of a singleton type of UserContext class for my asp.net MVC applications. This class allows me to store user data as a strongly-typed session object. I ran across this CodeReview question and wondered if it was necessary to be concerned about thread safety in this application context. Here's a simplification of my code

Facebook Connect Class with Singleton : Access token issue

好久不见. 提交于 2019-12-22 10:37:20
问题 I have created a singleton class called "LoginFacebook" which is meant to connect user to Facebook and to perform the different request. The problem is that I get an error about the access_token. Here it is : 3 : <CFString 0x4c552f0 [0xe50400]>{contents = "message"} = <CFString 0x4c55250 [0xe50400]>{contents = "An active access token must be used to query information about the current user."} First I connect to Facebook by making the following request from another View Controller : [

How do you choose between a singleton and an unnamed class?

假如想象 提交于 2019-12-22 10:26:41
问题 I'd use a singleton like this: Singleton* single = Singleton::instance(); single->do_it(); I'd use an unnamed class like this: single.do_it(); I feel as if the Singleton pattern has no advantage over the unnamed class other than having readable error messages. Using singletons is clumsier than using an unnamed class object: First, clients must first get a handle of the instance; second, the implementer of Singleton::instance() might need to consider concurrency. So why and how would you

Singleton PDO Model - Hierarchy

末鹿安然 提交于 2019-12-22 09:56:22
问题 Sorry for poor english, I'm Brazilian, and even worse talking about programming. Let's go. I have this: class DataBase extends PDO { /** * @var object PDO */ private static $instance; /** * Cria uma instância do PDO representando a conexão ao banco de dados e torna a instância disponível como "singleton" * * @param string $dsn O DSN completo, ex.: mysql:host=localhost;dbname=testdb * @param string $username O nome de usuário para a string DSN. Esse parâmetro é opcional para alguns drivers do

Double checked locking on C++: new to a temp pointer, then assign it to instance

主宰稳场 提交于 2019-12-22 08:27:24
问题 Anything wrong with the following Singleton implementation? Foo& Instance() { if (foo) { return *foo; } else { scoped_lock lock(mutex); if (foo) { return *foo; } else { // Don't do foo = new Foo; // because that line *may* be a 2-step // process comprising (not necessarily in order) // 1) allocating memory, and // 2) actually constructing foo at that mem location. // If 1) happens before 2) and another thread // checks the foo pointer just before 2) happens, that // thread will see that foo

What are the subtle differences between val and singleton objects?

丶灬走出姿态 提交于 2019-12-22 08:23:04
问题 A question in essence similar to subtle differences between val and def. I wonder what's the semantic difference between having a member singleton object: class Text { ... object Whitespace { def unapply(s :String) = if (s.forall(_.isWhitespace)) Some(s) else None } } and class Text { ... val Whitespace = new { def unapply(s :String) = if (s.forall(_.isWhitespace)) Some(s) else None } } I know how both translate to bytecode, but what can I do with one in the code that I can't with the other?

How to create a true singleton in java?

最后都变了- 提交于 2019-12-22 07:04:10
问题 I am facing a problem with my singleton when used across multiple class loaders. E.g Singleton accessed by multiple EJBs. Is there any way to create a singleton which has only one instance across all class loader? I am looking for pure java solution either using custom class loader or some other way. 回答1: The only way would be to make your singleton class be loaded by a single classloader - put that jar file in the bootclasspath, for example. A static variable is inherently tied to the