singleton

Is this a valid, lazy, thread-safe Singleton implementation for C#?

老子叫甜甜 提交于 2019-12-12 08:09:33
问题 I implemented a Singleton pattern like this: public sealed class MyClass { ... public static MyClass Instance { get { return SingletonHolder.instance; } } ... static class SingletonHolder { public static MyClass instance = new MyClass (); } } From Googling around for C# Singleton implementations, it doesn't seem like this is a common way to do things in C#. I found one similar implementation, but the SingletonHolder class wasn't static, and included an explicit (empty) static constructor. Is

Singleton using AtomicReference

邮差的信 提交于 2019-12-12 08:03:29
问题 Is it correct implementation of lazy-initializing singleton using AtomicReference? If no - what are the possible issues? import java.io.ObjectStreamException; import java.io.Serializable; import java.util.concurrent.atomic.AtomicReference; public class Singleton implements Serializable { private static final Singleton _instance = new Singleton(); private static AtomicReference<Singleton> instance = new AtomicReference<Singleton>(); private Singleton() { } public static Singleton getInstance()

Loading a Singleton's state from NSKeyedArchiver

我的梦境 提交于 2019-12-12 07:59:03
问题 I have a class that I've made into a singleton and am able to save it's state using an NSKeyedArchiver, but I can't wrap my head around pulling it's state back out. In the function that does the loading I have Venue *venue = [Venue sharedVenue]; NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; venue = [unarchiver decodeObjectForKey:@"Venue"]; [unarchiver finishDecoding];

Implementing Singleton in Java

旧巷老猫 提交于 2019-12-12 07:14:06
问题 Please note that I have gone through the below thread : What is an efficient way to implement a singleton pattern in Java? To summarize, there are important considerations while writing a singleton : Multiple threads access must not lead to multiple instances The singleton,if serializable, must ensure that de-serialization doesn't create a new instance In case of a reflection attack, an exception/error must be thrown. Now, as mentioned in the above thread too, using the enumeration for

Initialize Singletons in Spring Framework 3 MVC

流过昼夜 提交于 2019-12-12 07:02:28
问题 I am writing Spring 3.1.0 MVC based application. The problem is: i want to put some objects in a singleton object (current HttpServletRequest and HttpSevletResponse ) to use them in other objects(Spring Controllers). But couldn't do so. I tried to extend DispatcherServlet , overriding both doService and doDispatch . Also tried to implement own HandlerInterceptor . No result. Where can I initialize my singleton objects? And where is Spring Frameworks's entry point and destroy point (i.e. like

Android create reusable layout

自作多情 提交于 2019-12-12 06:46:21
问题 How to create a single pop up layout with textbox that can be called anywhere on the app. See below pseudo codes of what I mean; PopUpClass popup; private static Singleton instance = null; class Singleton { if(instance == null) { instance = new Singleton (); // Instantiate a class pop up class layout popup = new PopUpClass(); // I dont know how to create this one } return instance; } SomeClass { private buttonClicked { Singleton singleton = new Singleton(); PopUpClass popup = singleton.popup;

Symfony2 Service with an unique instance

℡╲_俬逩灬. 提交于 2019-12-12 06:39:23
问题 Please i need some help: I have the following services: SERVICE CONFIGURATION IN services.yml services: xpad.producto_repository: class: Xpad\ProductoBundle\Entity\ProductRepository factory_service: doctrine.orm.clientes_entity_manager factory_method: getRepository arguments: - Xpad\ProductoBundle\Entity\Product backend_cliente.producto_filters: class: Xpad\BackendClienteBundle\Filters\ProductFilters calls: - [setRepository, ["@xpad.producto_repository="]] scope: container AND THE CLASS FOR

Singleton variables in Jersey web service

十年热恋 提交于 2019-12-12 05:49:56
问题 I have a RESTful web service based on JAX-RS and Jersey . I have a bunch of GET and POST Methods and inside of it i need "global" variables. I have an initialization method with ServletContextListener and it's contextInitialized-Method for write some log files and do other stuff. In this method I want to declare variables wich I can access from anywhere in my application. here are my code: @WebListener public class MyServletContextListener implements ServletContextListener { //these are the

SQLite Handler class as singleton?

筅森魡賤 提交于 2019-12-12 05:40:07
问题 I am using SQLite and JAVA. I would like to know if its a good idea to make a singleton SQLite DB handler class. I have a normal SQLite DB handler class but I keep on running into NOT Closed DB or closed to early DB problems which I cant seem to figure out. So I thought that I might have to make a singleton class and just open it on creation and close it on destruction. Also all DB Queries will go through this class that is always open. Why would this be a bad idea? Side question...is it a

Setting Singleton property value in Firebase Listener

微笑、不失礼 提交于 2019-12-12 05:29:21
问题 I'm currently testing out Firebase along with a Singleton model I plan to use to access during the lifecycle of the whole app. I'm now stuck with something that seems really trivial but I can't figure it out for the life of me. I have a sample of the model I use: Bookmarks in firebase. public class BookSingleton { private static BookSingleton model; private ArrayList<BookMark> bookmarks = new ArrayList<BookMark>(); public static BookSingleton getModel() { if (model == null) { throw new