singleton

Java: How can I create an application that will only start if an instance of it doesn't exist and call the instance if it does exist?

☆樱花仙子☆ 提交于 2019-12-14 03:58:07
问题 I am trying to create a program with Java that can only have one instance of it running at a time. I am using Sockets and ServerSockets to try to achieve this. How the program is supposed to work is: The main method will check if any parameters have been passed, it will try to write the first parameter to the server, if it fails that means, that means that this is the only running instance, so it will open the ServerSocket and then start the frame. If it doesn't fail then the application is

Singleton Pattern in C [duplicate]

风格不统一 提交于 2019-12-14 03:43:09
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: How to create a Singleton in C ? Hello, if I have a structure definition as follows: struct singleton { char sharedData[256]; }; Can I impose the singleton pattern on instance variables of the above structure in C [not in C++]? 回答1: If you just forward declare your struct in the header file, it will be impossible for clients to create an instance of it. Then you can provide a getter function for your single

Guice: differences between Singleton.class and @Singleton

北慕城南 提交于 2019-12-14 03:40:24
问题 In Guice, what's the difference between: // Inside your AbstractModule subclass: @Override public void configure() { bind(Service.class).to(ServiceImpl.class).in(Singleton.class); } And: @Override public void configure() { bind(Service.class).to(ServiceImpl.class); } @Provides @Singleton public ServiceImpl providesService() { return new ServiceImpl(); } Are they both the same? When would you use one versus the other? Thanks in advance. 回答1: They are nearly identical. The @Singleton syntax is

Singleton on multiple JVMs using JNDI [closed]

那年仲夏 提交于 2019-12-14 03:25:24
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . It is said that Singleton is always on per JVM basis, but somebody asked me to create a singleton instance throughout multiple JVMs. I have found a solution to create an object on one JVM, register it with the

How does this Singleton-like web class persists session data, even though session is not updated in the property setters?

我是研究僧i 提交于 2019-12-14 03:23:00
问题 Ok, I've got this singleton-like web class which uses session to maintain state. I initially thought I was going to have to manipulate the session variables on each "set" so that the new values were updated in the session. However I tried using it as-is, and somehow, it remembers state. For example, if run this code on one page: UserContext.Current.User.FirstName = "Micah"; And run this code in a different browser tab, FirstName is displayed correctly: Response.Write(UserContext.Current.User

Use interface to provide Android context for singleton?

徘徊边缘 提交于 2019-12-14 03:06:54
问题 I've been looking through the forks of a popular library that keep an ApplicationContext as a member field within a singleton. This make the app memory intensive as the process is a bit expensive. Some forks claim to solve the problem by using an anonymous class implement an interface to provide the Context for the singleton in demand (here and here). In general, the code can be summarized like this: The interface: public interface FFbinaryContextProvider { Context provide(); } The singleton:

how to implement singleton in objective-c for iphone

廉价感情. 提交于 2019-12-14 02:37:16
问题 i want to make my object singleton in the iPhone application.What is the proper way of implementing this in objective-c. 回答1: Here's how I do it. +(MyClass*) mySingleton { static MyClass* theSignleton = nil; @synchronized([MyClass class]) { if (theSingleton == nil) { theSingleton = [[MyClass alloc] init]; } } return theSingleton; } That doesn't stop people from accidentally creating non singleton instances but I think it's better to design your class so that non singletons don't break the

Domain Driven Design issue regarding repository

◇◆丶佛笑我妖孽 提交于 2019-12-14 00:26:01
问题 I am trying to implement the DDD so I have created the following classes - User [the domain model] - UserRepository [a central factory to manage the object(s)] - UserMapper + UserDbTable [A Mapper to map application functionality and provide the CRUD implementation] My first question is that when a model needs to communicate with the persistent layer should it contact the Repository or the mapper? Personally I am thinking that it should ask the repository which will contact the mapper and

What are the Dangers of using a Singleton in a multithreaded application

元气小坏坏 提交于 2019-12-14 00:16:40
问题 I'm looking at using a singleton in a multithreaded Win service for doing logging, and wanted to know what are some of the problems I might encounter. I have already set up the get instance to handle syncing with private static volatile Logging _instance; private static object _syncRoot = new object(); private Logging(){} public static Logging Instance { get { if (_instance==null) { lock(_syncRoot) { if (_instance == null) { _instance = new Logging(); } } } return _instance; } } Is there

Is there any need to add volatile keyword to guarantee thread-safe singleton class in java?

扶醉桌前 提交于 2019-12-13 21:16:06
问题 According to this post, the thread-safe singleton class should look as below. But I'm wondering whether there's a need to add volatile keyword to static CrunchifySingleton instance variable. Since if the instance is created and stored in CPU cache, at which time it is not written back to main memory, meanwhile, another thread invoke on getInstance() method. Will it incur an inconsistency problem? public class CrunchifySingleton { private static CrunchifySingleton instance = null; protected