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

Is DocumentBuilder thread safe?

See the comments section for other questions about the same matter. Short answer for your question: no, it’s not ok to put these classes in a singleton. Neither DocumentBuilderFactory nor DocumentBuilder are guaranteed to be thread safe. If you have several threads parsing XML, make sure each thread has its own version of DoumentBuilder. You … Read more

Multiple instances of singleton across shared libraries on Linux

First, you should generally use -fPIC flag when building shared libraries. Not using it “works” on 32-bit Linux, but would fail on 64-bit one with an error similar to: /usr/bin/ld: /tmp/ccUUrz9c.o: relocation R_X86_64_32 against `.rodata’ can not be used when making a shared object; recompile with -fPIC Second, your program will work as you expect … Read more

Singleton and unit testing

You could use reflection to reset your singleton object to prevent tests from affecting each other. @Before public void resetSingleton() throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException { Field instance = MySingleton.class.getDeclaredField(“instance”); instance.setAccessible(true); instance.set(null, null); } Ref: unit-testing-singletons

Monostate vs. Singleton

monostate and singleton are two faces of the same medal (global state): monostate forces a behaviour (only one value along all class instances) singleton forces a structural constraint (only one instance) singleton usage is not transparent i.e.: Singleton singleton = Singleton.getInstance(); monostate usage is transparent i.e.: MonoState m1 = new MonoState(); MonoState m2 = new … Read more

Unit testing with singletons

Have a look at the Google Testing blog: Using dependency injection to avoid singletons Singletons are Pathological Liars Root Cause of Singletons Where have all the Singletons Gone? Clean Code Talks – Global State and Singletons Dependency Injection. And also: Once Is Not Enough Performant Singletons Finally, Misko Hevery wrote a guide on his blog: … Read more

Where exactly the Singleton Pattern is used in real application?

Typically singletons are used for global configuration. The simplest example would be LogManager – there’s a static LogManager.getLogManager() method, and a single global instance is used. In fact this isn’t a “true” singleton as you can derive your own class from LogManager and create extra instances that way – but it’s typically used as a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)