singleton

Using RequireJS, how do I pass in global objects or singletons around?

五迷三道 提交于 2019-12-17 07:02:45
问题 Let's say I am writing code at the main page level and 2 dependencies require the same instance of an object and also state that as a dependency. What is the appropriate way to go about this? Basically what I want to do is say, "If this dependency isn't loaded... then load it. Otherwise, use the same instance that was already loaded and just pass that one." 回答1: You would make that a module-level variable. For example, // In foo.js define(function () { var theFoo = {}; return { getTheFoo:

Any reason not use use a singleton “variable” in Swift?

我与影子孤独终老i 提交于 2019-12-17 06:50:57
问题 For Sept 2015, here's exactly how you make a singleton in Swift: public class Model { static let shared = Model() // ( for ocd friends ... private init() {} ) func test()->Double { return 3.33 } } then elsewhere... // file ViewController.swift, say import UIKit class ViewController:UIViewController { override func viewDidLoad() { super.viewDidLoad() print("view controller loaded!") print("singleton test! \( Model.shared.test() )") } } No problem. However. I add this little thing... public let

Singleton pattern in nodejs - is it needed?

£可爱£侵袭症+ 提交于 2019-12-17 04:13:01
问题 I recently came across this article on how to write a singleton in Node.js. I know the documentation of require states that: Modules are cached after the first time they are loaded. Multiple calls to require('foo') may not cause the module code to be executed multiple times. So it seems that every required module can be easily used as a singleton without the singleton boilerplate-code. Question: Does the above article provide a round about solution to creating a singleton? 回答1: This has

Singleton by Jon Skeet clarification

别等时光非礼了梦想. 提交于 2019-12-17 03:45:42
问题 public sealed class Singleton { Singleton() {} public static Singleton Instance { get { return Nested.instance; } } class Nested { // Explicit static constructor to tell C# compiler // not to mark type as beforefieldinit static Nested() {} internal static readonly Singleton instance = new Singleton(); } } I wish to implement Jon Skeet's Singleton pattern in my current application in C#. I have two doubts on the code How is it possible to access the outer class inside nested class? I mean

Singleton Design Pattern: Pitfalls [closed]

吃可爱长大的小学妹 提交于 2019-12-17 03:09:20
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 2 years ago . Currently I have been very interested in this "design pattern". I am not sure though if there are downfalls using this strict global state implementation. So, when do you think not to practice singleton in an application? 回答1: Singleton is generally a bad idea if you are doing

Singleton class with several different classloaders

断了今生、忘了曾经 提交于 2019-12-17 02:29:38
问题 E.g I have class Singleton with static field instance : public class Singleton { private static Singleton instance; // other code, construct, getters, no matter } I can load this class twice with two different classloaders. How could I avoid it? It is unsafe and dangerous. Also, if I set instance to null, would it set to null for both classes? Singleton singleton = Singleton.getInstance(); singleton = null; 回答1: If you want a true Singleton across classloaders, then you need a common parent

Thread Safe singleton class

[亡魂溺海] 提交于 2019-12-17 02:19:08
问题 This question was migrated from Software Engineering Stack Exchange because it can be answered on Stack Overflow. Migrated 6 years ago . I wrote a below Singleton class. I am not sure whether this is thread safe singleton class or not? public class CassandraAstyanaxConnection { private static CassandraAstyanaxConnection _instance; private AstyanaxContext<Keyspace> context; private Keyspace keyspace; private ColumnFamily<String, String> emp_cf; public static synchronized

How is Meyers' implementation of a Singleton actually a Singleton

半城伤御伤魂 提交于 2019-12-17 02:10:53
问题 I have been reading a lot about Singletons, when they should and shouldn't be used, and how to implement them safely. I am writing in C++11, and have come across the Meyer's lazy initialized implementation of a singleton, as seen in this question. This implementation is: static Singleton& instance() { static Singleton s; return s; } I understand how this is thread safe from other questions here on SO, but what I don't understand is how this is actually a singleton pattern. I have implemented

Using Singleton design pattern for SQLiteDatabase

≯℡__Kan透↙ 提交于 2019-12-17 00:27:15
问题 I'm rather newbie on Android, and I'm working on a simple application to get some basic experience. My app is pretty simple and consists among other things of a broadcast receiver and some activities. Both components make use of a single database, so in theory it could happen that both try to access the db concurrently. Currently I'm simply instantiating the db object (which is-a SQLite db helper class) each time I need it, and performing the needed operations: query, insert, etc. From what I

TDD-friendly Singleton-like class

若如初见. 提交于 2019-12-14 04:25:37
问题 I have repository class that is used by at least 2 other classes. This repository class needs to be initialized - which is high in cost (querying database). Now, I create separate instances of repository wherever I need it. The thing is, that everytime I create repository it has to be initialized. How to design such repository to be TDD-friendly? The first thing in my mind was Singleton but it's not the solution. 回答1: Do you use any type of IOC container? Unity is my container of choice, and