singleton

Singleton Pattern In PHP… How Can I Save State Between Requests

浪尽此生 提交于 2019-12-07 12:36:58
问题 With the use of static variables and the singleton pattern, I thought that it would be easy enough to create a simple shopping cart that remembered what items where in the cart when another page was loaded. I am having a problem of the shopping cart not remembering what was already in it when the page is refreshed. Is there a problem with my code below or should I just use globals or a mysql database. What is the best way to go about storing state.. <?php //create a singleton class class

Ensure Singleton call db query only once

时光怂恿深爱的人放手 提交于 2019-12-07 11:45:28
问题 I'm trying to create one object that will be responsible for reading all users access settings. I've created class as so: public class SettingsManager { private static string _connString = @"Data Source=MyDB;Initial Catalog=IT;Integrated Security=True;Asynchronous Processing=true;"; private const string _spName = "SettingTest"; private IEnumerable<string> _mySettings; private static readonly Lazy<SettingsManager> _instance = new Lazy<SettingsManager>(() => new SettingsManager()); private

why there is two instances of a singleton with Google Guice dependency injection framework

会有一股神秘感。 提交于 2019-12-07 07:11:38
问题 I am developper in a compagny using Google Guice as dependency injection framework in a MVC architecture. The system use a Singleton model. We just discover that there's two instances of the model in the project and this is problem. I know that Singleton is never the better way, but I am not the project designer. Google Guice is the only model initializer so that there is not "new". Google Guice sometimes inject the model by fields injection and sometime in the constructor itself. But, the

Self destructing singleton design pattern iOS

放肆的年华 提交于 2019-12-07 07:03:23
问题 I recently came across an issue in which I only wanted one instance of a particular object to exist, and exist for only the brief period of time it needed to perform a specific operation. Its operation was asynchronous so ARC would dealloc it at the end of the run loop if I didn't hold a reference to it. If I did hang onto it I would need delegate callbacks or notifications to know when it was done to release it. The object needed to download several images and other data and cache it to disk

iOS - Can't access Swift Singleton from Objective-C

我只是一个虾纸丫 提交于 2019-12-07 06:46:09
问题 I can't access my Swift Singleton Class from my Objective-C ViewController. Xcode does recognize my Swift Class, it builds, so I don't think it is a bridging header issue. Here is my Swift Singleton Class : @objc class MySingleton : NSObject { static let shared = MySingleton() private override init() {} } And then in my .m file I import this header : #import "myProject-Swift.h" And use the Singleton this way : MySingleton *testSingleton = [MySingleton shared]; or this way : [MySingleton

I'm trying to get a UILabel to scroll inside of a UIScrollView but it doesnt scroll

情到浓时终转凉″ 提交于 2019-12-07 04:49:55
问题 This is in my .m - (void)viewDidLoad { [super viewDidLoad]; [self.scrollView addSubview:self.contentView]; self.scrollView.contentSize = self.contentView.bounds.size; NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; NSMutableArray *arr = [[NSMutableArray alloc]init]; arr = [Singleton getArray]; NSString *str = [arr componentsJoinedByString:@"\n"]; summaryLabel.text = str; } This is in my .h @interface TotalViewController : UIViewController { UIScrollView *scrollView;

C# Singleton Pattern Designs for ThreadStatic

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-07 04:21:42
问题 I want to figure out about singleton pattern designs. I want to create seperated instances for per thread from my singleton class. So I provided two designs below. It is Working class Program { static void Main(string[] args) { Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode())); Task.Factory.StartNew(() => Console.WriteLine(SingletonClass.Instance.GetHashCode())); Console.ReadLine(); } } public sealed class SingletonClass { [ThreadStatic] private static

Singleton with subclassing in java

自闭症网瘾萝莉.ら 提交于 2019-12-07 03:54:19
问题 The most common way of implementing a singleton in java is to use a private constructor with a public accessor method of the form -- public class Singleton { private static Singleton instance = null; private Singleton() { } public static synchronized Singleton getInstance(){ if (instance == null) { instance = new Singleton(); } return instance; } } However, since the constructor is private, it prevents subclassing the singleton. Is there any way in which we can make a singleton which allows

Implementing singleton inheritable class in C# [duplicate]

假装没事ソ 提交于 2019-12-07 03:35:40
问题 This question already has answers here : Abstract base class to force each derived classes to be Singleton (7 answers) Closed 6 years ago . I found that in C# you can implement a Singleton class, as follows: class Singleton { private static Singleton _instance; public static Singleton Instance { get { return _instance ?? (_instance = new Singleton()); } } protected Singleton() { } } Which works for instances of type Singleton , i.e: var a = Singleton.Instance; var b = Singleton.Instance;

How to fix a purported lack of an “explicit instantiation declaration” when compiling a CRTP Singleton with Clang?

一曲冷凌霜 提交于 2019-12-07 03:34:12
问题 We're using the curiously recurring template pattern to implement singletons. However, with recent Clang versions we're getting a -Wundefined-var-template warning. The suggested fix to which is to add an "explicit instantiation declaration". I attempted to do this, but then I get errors about "explicit specialization after instantiation" in the compilation unit where the definition of the singleton template class member variable is. What's the appropriate construct to fix the issue