singleton

How can I use jUnit to test a class that uses a singleton?

a 夏天 提交于 2019-12-25 02:41:02
问题 I am developing a simulator for a machine. It reads a file in, stores it in a string (as a binary number) to an array that is supposed to simulate the memory of a computer. Then it gets passed to an interpreter that takes gets the first element of the array, and based on the first 4 characters it gets sent to another class. An example of one of those classes is below: public class Add { private String cell; Add() { cell = null; } /** * Method that performs the operation add. */ public void

How to implement Singleton in Django

 ̄綄美尐妖づ 提交于 2019-12-25 02:33:18
问题 I have an object that need to be instantiated ONLY ONCE. Tried using redis for caching the instance failed with error cache.set("some_key", singles, timeout=60*60*24*30) but got serialization error, due the other thread operations: TypeError: can't pickle _thread.lock objects But, I can comfortably cache others instances as need. Thus I am looking for a way to create a Singleton object, I also tried: class SingletonModel(models.Model): class Meta: abstract = True def save(self, *args, *

Stack overflow error in singleton pattern

删除回忆录丶 提交于 2019-12-25 02:22:04
问题 I have implemented Single Pattern. Here is my code i am getting the an error when i call the Test.BuildData() function. Please help public class WordDataItem { public string Word { get; set; } public string Definition { get; set; } public int WordGroupKey { get; set; } } public class WordDataGroup { public List<WordDataItem> listItem = new List<WordDataItem>(); public int GroupKey { get; set; } } public sealed class WordDataSource { private static WordDataSource _dataSoruce; private List

java singleton thread safe

瘦欲@ 提交于 2019-12-25 01:48:49
问题 Ok, here is my problem. I need to create a socket program that can handle multiple connection from my client apps (lets call it apps1). I handle this using thread (so each connection was thrown into a new thread) The problem is I can accept request from all open connection but when I want to send a response , I must send it through the latest connection only. So if I have 3 connections (con1,con2,con3) I can accept request from con1, con2 and con3 but I must send the response through con3

When to use a singleton?

旧时模样 提交于 2019-12-25 00:42:31
问题 I have this code: class MyController { public function newUserAction() { $view = new View('myfrontend'); if($this->request->isPost()) { $form = new MyForm; $posts = $this->request->getPosts(); if($form->isValid($posts)) { //... } } $view->display(); } } So, everytime the form is not filled in correctly, the process starts again and so every time there is a "new View('myfrontend')" ect. But is that a good thing? To have a new view object again and again and again. Ain't it better to work with

Should i avoid a singleton as datasource for a TabBarController (and how can i)

社会主义新天地 提交于 2019-12-24 19:28:21
问题 I got a TabBarController as initial scene; every tab uses basically the same information. This information is only available at runtime and is retrieved via json. The information will not be altered by my app but may be altered on the other side and could vary the next time i would refresh it. Is this a valid case for a singleton? I thought about loading the information before the TabBarController gets created and pass it on to its tabs, but I am confronted with several downsides now: (with a

NSmanaged context threads

半城伤御伤魂 提交于 2019-12-24 17:40:50
问题 I use a singleton for working with arrays etc. cross the views in the application. To initialize the singleton and the NSManagedObjectContext , so that I can fetch objects, I use: +(DataControllerSingleton *)singleDataController { static DataControllerSingleton * single=nil; @synchronized(self) { if(!single) { single = [[DataControllerSingleton alloc] init]; NSManagedObjectContext *context = [single.fetchedResultsController managedObjectContext]; single.masterCareList = [[NSMutableArray alloc

android singleton activity

夙愿已清 提交于 2019-12-24 16:48:02
问题 The main activity of the app is TabActivity that contains some OneActivity It is necessary to call from another part of app the OneActivity not creating the another instance of it, just calling onResume() of the one that lies in TabActivity Tried set different launchMode ("singleTop", "singleTask", "singleInstance") and set flags for intent: intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); How to do it not creating a new instance of activity? 回答1: Try the

Generic Singleton and share data between pages

筅森魡賤 提交于 2019-12-24 16:29:19
问题 To share data (complexe data ) between pages in my windows phone 8 application I want to implement a singleton, but I want it to be generic, is it possible? I suppose that it creates a new instance for each type isn't it? public sealed class NavigationContextService<T> { private static readonly NavigationContextService<T> instance = new NavigationContextService<T>(); private NavigationContextService() { } public static NavigationContextService<T> Instance { get { return instance; } } public

c++ singleton implementation STL thread safe

自古美人都是妖i 提交于 2019-12-24 15:33:42
问题 I've been trying to implement a singleton with some C++11 features from STL. I read about a few implementations and I found this pretty good: http://silviuardelean.ro/2012/06/05/few-singleton-approaches/ I made a few changes and got the bellow code working on VS2013, but I still would like to know: a) Is this implementation thread-safe? b) Is it ok (good practice) to return a shared_ptr from GetInstance instead of a reference? PS.: My singleton is ment to be an OpenGL interface (that's the