singleton

iPhone help with singleton class

心不动则不痛 提交于 2019-12-11 17:53:56
问题 Greetings, I'm looking at Matt Gallagher's macro for creating singleton classes. http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html Basically, I have an app with multiple views and I want to be able to access "global" data from each of these views using a singleton class. I basically have three strings I want to access in this class: NSString *uname, NSString *details and NSString *selectedDetails. Do I need to make three singleton classes with a static variable in

Cocos2d with ARC: what is the best implementation of singleton pattern for GameScene when having multiple levels?

孤者浪人 提交于 2019-12-11 17:33:07
问题 EDIT: I am using ARC In my code (based on the ShootEmUp example in this book, which I highly reccomend, source code in chapter 8 available here) I often use the trick of accessing the GameScene via: +(GameScene*) sharedGameScene; which returns a reference to the static instance of GameScene and is used by all children of GameScene (e.g. ShipEntity, InputLayer.. etc..) to dialog with GameScene (EDIT: aka singleton instance of GameScene ). To create multiple levels I thought of implementing a

C++ Singleton/Active Object Paradigm

纵然是瞬间 提交于 2019-12-11 16:21:47
问题 I was wondering how you would make a class where you create several instances i.e Session o1 = new Session(); Session o2 = new Session(); You could then make these sessions the active session like so. o1.makeActiveSession(); Session::setActiveSession(o2); Then at any point in my code I could go: Session::getActiveSession(); and it would return the active session object or create new one if one doesn't exist. Only one session can be the active session at any one time, so if a session is told

Ruby - Ensure only one class object

断了今生、忘了曾经 提交于 2019-12-11 16:18:37
问题 I have a Model Bot and I would like to ensure that there is only one Bot object in my database. I also need to make sure it is persisted and not tampered with. My original thought was to do this in a migration, one that would follow the :bots table migration. It would include a line that is something like: Bot.all.size == 0 ? Bot.create! : nil Maybe this would prevent the AR object from being messed with in future migrations? BONUS: Would be awesome to be able to have instant and global

What are the chances a singleton's 'instance' variable becomes null in an android app while switching between Activities?

流过昼夜 提交于 2019-12-11 16:06:31
问题 I have a singleton, typical design with a static 'mInstance' to hold the global state. I notice that sometimes, while switching between activities, the mInstance variable becomes null and requires to be re-instantiated, causing all data to go empty. Is this expected or am I doing something wrong? Is there really a chance that the static variables of a singleton would be nullified in such a scenario? I seriously doubt it and would like to hear some opinions. Code is pasted: public class

Node.js & Typescript Singleton pattern : seems like singleton is not shared between singleton users

百般思念 提交于 2019-12-11 15:11:31
问题 I am using Typescript (2.5.3) in a backend Node.js application, and trying to figure out where my coding mistake is, in the following code. Briefly, I have one main class, and several other classes that use this main class : DataBase ( main class ) : which is a singleton ( see export let DB = DataBase.Instance; below ) whose role is simply to connect to a Mongo database, and set a flag if connection successful or not. This flag will be used by other classes to know whether connection was

Python, Circular Dependencies, and Singletons

最后都变了- 提交于 2019-12-11 13:54:33
问题 I've dug myself into quite a hole here. I'm working on a Python/Kivy app in PyDev. The app runs off of many systems (about 10), so I shoved them into an engine to handle everything. For ease of access, I grab the engine via (the worst) singletons main.py #main.py from code import engine class MyApp(App): def build(self): engine.GetInstance().Initialize() if __name__ == '__main__': MyApp().run() engine.py #engine.py from code import system1 from code import system2 gEngineInstance = None def

One instance of Report per instance of Simulation: use a singleton?

点点圈 提交于 2019-12-11 13:52:44
问题 I have written code that runs many different simulations, each in its own Simulation object. To extract results from a Simulation , we first have to ask Simulation to create an instance of Report (as one of Simulation 's children). Even though a Simulation can contain many instances Report , the creation process is quite expensive, so if there already is a Report in that particular Simulation , I want to re-use it rather than create a new one. The Report instances are accessed from many

Using MKReverseGeocoder in a singleton class (ARC)

China☆狼群 提交于 2019-12-11 13:27:38
问题 I'm in the process of trying to create a singleton class for weather functions (all encompassed) so that I can change/update/recall weather data throughout my entire app in a single allocated object. I have it working except for one little weird bug. I'm using MKReverseGeocoder on devices running iOS < 5. CLGeocoder works fine on iOS 5+. Basically, this is what happens in the app: In the app delegate, at launch, I create the shared instance of my weather singleton. This instance doesn't do

a request level singleton object in asp.net

柔情痞子 提交于 2019-12-11 12:49:25
问题 I trying to write a kind of pseudo singleton implementation. I want it to work similar to how HttpContext does work, where I can get an instance to the context doing something as simple as: var ctx = HttpContext.Current; So my implementation goes something like this: public class AppUser { public string Username { get; set; } public string[] Roles { get; set; } public AppUser() { var appuser = HttpContext.Session["AppUser"] as AppUser; if(appuser == null) throw new Exception("User session has