singleton

Disposing a singleton instance (C#)

允我心安 提交于 2019-12-06 07:29:35
If a singleton implements IDisposable, what is the right way of disposing and re-creating an instance? One way would be to keep _disposed flag and check for it in the Instance property, but I'm not sure it is the right way to do this. A simple example: public sealed class Singleton: IDisposable { private static Singleton _instance; private object _lock; private UnmanagedResource _unmanaged; private bool _disposed; private Singleton() { _unmanaged = new UnmanagedResource(); _disposed = false; _lock = new object(); } public UnmanagedResource Unmanaged { get { return _unmanaged; } } public static

Singleton Class iPhone

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-06 06:48:39
问题 Ok, I'm trying to avoid global variables, so I read up on singleton classes. This is a try to set and read a mutable array, but the result is null. //Content.h @interface Content : NSObject { NSMutableArray *contentArray; } + (Content *) sharedInstance; - (NSMutableArray *) getArray; - (void) addArray:(NSMutableArray *)mutableArray; @end . //Content.m @implementation Content static Content *_sharedInstance; + (Content *) sharedInstance { if (!_sharedInstance) { _sharedInstance = [[Content

How do you share Java Caching System (JCS) resource across multiple EJB

痴心易碎 提交于 2019-12-06 06:42:06
问题 I am using JCS to store the ldap search results which should be shared by multiple EJB. I have created a singleton class to initialize JCS only once but due to EJB's classloader, it's been initialized multiple times with its own copy. so search resources are not shared. How are you guys resolving issue where you need to share the cache across multiple beans? I am looking for cache within JVM. (Not the remote e.g memcached etc.). Glassfish is used as an application server. 回答1: I haven't been

Class inheritance in PHP with Singleton Pattern only works if the instance variable in the inherited class is reinitialized. But why?

試著忘記壹切 提交于 2019-12-06 06:03:35
I have a main class with the singleton function instance() and the associated variable $instance . Now I create several subclasses and let the main class inherit. I do not re-define the singleton function and variable, because of the useful inheritance. Unfortunately, each instance points to the 1st subclass. Only when in the subclasses the $instance variable is initialized to null it works, but why? With the keywords static and not self , the scope should remain in the subclass. Here is the source code for a better understanding of what I mean: // PHP Version 7.0 // Don't work as expected:

What is the best way to implement a singleton pattern class in Actionscript 3?

≡放荡痞女 提交于 2019-12-06 05:35:23
问题 Since AS3 does not allow private constructors, it seems the only way to construct a singleton and guarantee the constructor isn't explicitly created via "new" is to pass a single parameter and check it. I've heard two recommendations, one is to check the caller and ensure it's the static getInstance(), and the other is to have a private/internal class in the same package namespace. The private object passed on the constructor seems preferable but it does not look like you can have a private

Memory-leak free Singleton with context

元气小坏坏 提交于 2019-12-06 04:50:10
I am trying to implement the following singleton pattern: SingletonClass.getInstance(context).callMethod() While there are a variety of tutorials that explain how to make singletons in Kotlin, none of them address the fact that holding a context in a static field will cause memory leaks in Android. How do I create the above pattern without creating a memory leak? Update: Here is my implementation of CommonsWare's solution #2. I used Koin. Singleton class: class NetworkUtils(val context: Context) { } Application Class: class MyApplication : Application() { val appModule = module { single {

Instantiating and using an enum singleton

徘徊边缘 提交于 2019-12-06 04:44:42
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 a = elvis.getAge(); elvis.leaveTheBuilding(); // and so on, using elvis I'm tempted to use the latter

C++ Singleton Design pattern alternatives

谁说胖子不能爱 提交于 2019-12-06 04:40:25
I hate to beat a dead horse, that said, I've gone over so many conflicting articles over the past few days in regards to the use of the singleton pattern. This question isn't be about which is the better choice in general, rather what makes sense for my use case. The pet project I'm working on is a game. Some of the code that I'm currently working on, I'm leaning towards using a singleton pattern. The use cases are as follows: a globally accessible logger. an OpenGL rendering manager. file system access. network access. etc. Now for clarification, more than a couple of the above require shared

How can I create a singleton in Apache Tomcat startup

蓝咒 提交于 2019-12-06 04:30:43
问题 I need to create a singleton when Apache Tomcat starts, so that I can access them with servlets. The singleton defines what response will the servlets give. I wanted to know whether tomcat has a constructor so I can add code so that the singleton can be created. *edit: after searching a little, I found that I could try using a web service (JAX-WS). I don't know how the jvm treats instances in the web-service though. Can I access the same object on different connections to the WS? I used

How static vs singleton classes work (databases)

房东的猫 提交于 2019-12-06 03:51:06
I am confused on how a singleton model vs a static model works for database connections. My friend created a "static" class and showed me it but it did not make any sense on how it was static. I kind of understand the singleton method of how to create a database connection, but i'm not sure it meets my goal. The main thing I want to do is cut down on the number of connections opened to MYSQL. I have a class with a function that calls the database quiet frequently, and there is no reason for it to make a new connection each time someone requests something that requires the database. Could