singleton

Singleton object- In static block or in getInstance(); which should be used

穿精又带淫゛_ 提交于 2019-12-07 03:25:14
问题 Below are two ways to implement a singleton. What are the advantages and disadvantages of each? Static initialization: class Singleton { private Singleton instance; static { instance = new Singleton(); } public Singleton getInstance() { return instance; } } Lazy initialization is: class Singleton { private Singleton instance; public Singleton getInstance(){ if (instance == null) instance = new Singleton(); return instance; } } 回答1: Synchronized Accessor public class Singleton { private static

Singleton pattern implementation from Wikipedia

牧云@^-^@ 提交于 2019-12-07 02:59:20
问题 I am referring to the solution for the Singleton Pattern by Bill Pugh on Wikipedia: public class Singleton { // Private constructor prevents instantiation from other classes private Singleton() {} /** * SingletonHolder is loaded on the first execution of Singleton.getInstance() * or the first access to SingletonHolder.INSTANCE, not before. */ private static class SingletonHolder { private static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return

Why is this double-checked locking correct? (.NET)

a 夏天 提交于 2019-12-07 02:30:10
问题 I have read a lot about the dangers of double checked locking and I would try hard to stay away of it, but with that said I think they make a very interesting read. I was reading this article of Joe Duffy about implementing singleton with double checked locking: http://www.bluebytesoftware.com/blog/PermaLink,guid,543d89ad-8d57-4a51-b7c9-a821e3992bf6.aspx And the (variant of) solution he seemed to propose is this: class Singleton { private static object slock = new object(); private static

Overriding operator new/delete in derived class

江枫思渺然 提交于 2019-12-07 02:18:10
问题 I have a stateless, abstract base class from which various concrete classes inherit. Some of these derived classes are stateless as well. Because many of them are created during a run, I'd like to save memory and overhead by having all stateless derived classes emulate a singleton, by overriding operator new()/delete(). A simplified example would look something like this: #include <memory> struct Base { virtual ~Base() {} protected: Base() {} // prevent concrete Base objects }; struct D1 :

What is the use of Singleton class in Volley

眉间皱痕 提交于 2019-12-07 01:47:12
问题 I used Volley library to download a image with the help of Singleton class. Problem : If I used a single ton class I can download a image successfully with in a time and also I noticed that image is downloaded successfully with out using the single ton class also. Will you please any one tell me what are the benefits with the singleton class into my code . ---------------------Code With Singleton Class -------------------- MainActivity.java public class MainActivity extends AppCompatActivity

ExtJS: Using remotely loaded singleton values for store definition

萝らか妹 提交于 2019-12-07 01:37:00
问题 I'm having some trouble trying to figure out how to do this (if it's even possible). I have an app which uses parse.com to store it's data, the thing is I want each user to have a different parse.com account so their data sets don't intersect whatsoever. So I created a singleton (Settings) which stores the user's appId and apiKey, which are loaded from a general parse.com account which is managed by me and contains each user's email, appId and apiKey, so when they log into the app it gets the

Auto-property initializer Singleton implementation

断了今生、忘了曾经 提交于 2019-12-06 22:46:44
问题 So, with the brand new C# 6 we got those neat auto-property initializers. I thought I might as well take advantage of these to make more concise singletons than ever. Apparently someone else got that idea, too. public sealed class Singleton { public static Singleton Instance { get; } = new Singleton(); private Singleton() { /* some initialization code */ } } My questions are: How thread-safe it is? How lazy it is, or when the instance is actually created? (not a priority, but it would be good

Have I used Singleton with Realm Database correct?

坚强是说给别人听的谎言 提交于 2019-12-06 22:31:09
问题 I have an app where the user can create/delete/edit to a list from a DialogFragment. Before I had a method like this in my DialogFragments: MainActivtity.adapter.add(String name, String location, double price); adapter is my adapter object for the recyclerView. The class for the adapter had my create/delete/edit methods for the items in the recyclerView. Which was called like shown above which also is a horrible way to call mehtods as I understand. So I choose to put all these CRUD methods in

Establishing database connection in php using singleton class

我的未来我决定 提交于 2019-12-06 22:07:30
Can anybody please guide me with a sample code to establish a database connection in php using singleton class. class DatabaseSingleton { // [Singleton] private static $instance = null; public static function getInstance() { if (!self::$instance) { self::$instance = new self(); } return self::$instance; } private function __clone(){} // [/Singleton] private $connection = null; private function __construct() { $this->connection = mysql_connect('localhost','root','admin'); if ($this->connection) { mysql_select_db('my_database'); } } // // crud operations go here. // } $db = DatabaseSingleton:

Singleton in scope of a request in rails [closed]

时间秒杀一切 提交于 2019-12-06 20:56:22
问题 Closed . This question needs details or clarity. It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post. Closed 5 years ago . Using singleton mixin from rails I could create a singleton class in the scope of rails app. But I was wondering Is there a way to create it in the scope of a particular request ? 回答1: Since a request is tied to a thread, you can use Thread local store: class RequestSingleton def self.instance