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

You would make that a module-level variable. For example, // In foo.js define(function () { var theFoo = {}; return { getTheFoo: function () { return theFoo; } }; }); // In bar.js define([“./foo”], function (foo) { var theFoo = foo.getTheFoo(); // save in convenience variable return { setBarOnFoo: function () { theFoo.bar = “hello”; … Read more

Android: Cannot perform this operation because the connection pool has been closed

Remove db.close(); If you try another operation after closing the database, it will give you that exception. The documentation says: Releases a reference to the object, closing the object… Also, check out Android SQLite closed exception about a comment from an Android Framework engineer which states that it is not necessary to close the database … Read more

Thread safety in Singleton

Josh Bloch recommends using a single-element enum type to implement singletons (see Effective Java 2nd Edition, Item 3: Enforce the singleton property with a private constructor or an enum type). Some people think this is a hack, since it doesn’t clearly convey intent, but it does work. The following example is taken straight from the … Read more

Javascript: best Singleton pattern [duplicate]

(1) UPDATE 2019: ES7 Version class Singleton { static instance; constructor() { if (instance) { return instance; } this.instance = this; } foo() { // … } } console.log(new Singleton() === new Singleton()); (2) ES6 Version class Singleton { constructor() { const instance = this.constructor.instance; if (instance) { return instance; } this.constructor.instance = this; } … Read more

Singleton in Swift

The standard singleton pattern is: final class Manager { static let shared = Manager() private init() { … } func foo() { … } } And you’d use it like so: Manager.shared.foo() Credit to appzYourLife for pointing out that one should declare it final to make sure it’s not accidentally subclassed as well as the … Read more