How to Store a Reference of a Singleton Class

Is there a reason to keep a reference to a Singleton instance in another class in Java?

In this example, no, there is no benefit.

However, if you are using dependency injection where your class takes its dependencies as constructor arguments, passing in a singleton instance could be very useful, and in that case you would have no option but to store the reference.

How to keep the reference of the singleton class object in the companion object, Kotlin

Use lateinit to indicate that the field will be initialized later.

   companion object {
lateinit var androidLauncher: AndroidLauncher
}

Singleton Pattern: getInstance() vs Passing the singleton object?

If we assume for a moment that SingletonClass is not a singleton and we do not get an instance by calling static method we face another problem, how to link these classes together. This problem is solved by Dependency Injection and this concept is well described here:

  • Inversion of Control Containers and the Dependency Injection pattern
  • Unit Testing 101: Inversion Of Control

After reading above it should be easy to choose option .1 where all classes get in constructor references to required dependencies. You can even create an interface for a behaviour you need and implement it in SingletonClass. Now you see, that a fact that class implements Singleton pattern does not make it special and we should inject them like other classes. All benefits from using DI you can apply to your class.

Just compare it with .3 and you need to write a test where you need mock something. It would be more unpleasant task then in case of .1.

Can I return a reference to a static class (singleton) instance within that class in C++?

It is possible to have a static instance, but it is not desirable to have it at class level because it may then happen that it is not yet initialized on access (due to the not completely defined static initialization order). Instead you should use a function-local static:

class Foo {
private:
Foo() { }
Foo(const Foo &rhs);
Foo &operator=(const Foo &rhs);

public:
inline static Foo &Instance(void) {
static Foo singleton;
return singleton;
}
};

That way it is guaranteed to get initialized the first time the Instance function is called.

Singleton with Reference Variable

Problem.

The code you've shown shouldn't compile: you can't bind a reference to non-const to an rvalue.

Examples with Visual C++ 11.0 and g++ 4.7.1:


[D:\dev\test]
> cl foo.cpp
foo.cpp
foo.cpp(27) : warning C4239: nonstandard extension used : 'initializing' : conversion from 'Priority' to 'Priority &'
A non-const reference may only be bound to an lvalue
foo.cpp(28) : warning C4239: nonstandard extension used : 'initializing' : conversion from 'Priority' to 'Priority &'
A non-const reference may only be bound to an lvalue
foo.cpp(29) : warning C4239: nonstandard extension used : 'initializing' : conversion from 'Priority' to 'Priority &'
A non-const reference may only be bound to an lvalue

[D:\dev\test]
> gnuc foo.cpp
foo.cpp:27:48: error: invalid initialization of non-const reference of type 'Priority&' from an rvalue of type 'Priority'
foo.cpp:28:50: error: invalid initialization of non-const reference of type 'Priority&' from an rvalue of type 'Priority'
foo.cpp:29:47: error: invalid initialization of non-const reference of type 'Priority&' from an rvalue of type 'Priority'

[D:\dev\test]
> _

Tool usage.

With Visual C++, use option /W4 (warning level 4) to get the warnings shown above.

You can put this and other “make it standard-conforming” options in the environment variable CL (and ditto, LINK for the Microsoft linker, e.g. /entry:mainCRTStartup as a default linker option).


[D:\dev\test]
> echo %CL%
/nologo /EHsc /GR /W4 /FI"progrock/cppx/macro/c++11.for_msvc_11.h"

[D:\dev\test]
> _

C++ fix for the problem.

Instead of lvalue references you could use rvalue references, but although Visual C++ is happy with that, g++ then complains about the private destructor.

So intead of the reference idea – what purpose was it meant to serve? – you can simply use ordinary static data members.

However, with ordinary static data members, as opposed to e.g. Meyers' singletons, you run the risk of banging into the static initialization order fiasco. So if you absolutely must provide globals, I recommend using singletons for that. And as for the kind of singleton, using the simplest possible solution is often the best, which means Meyers' singletons (I just linked the first reasonably-looking google result).

How to pass a singleton reference to ten sibling objects

Option 1 provides for less noise and duplication in your code.



Related Topics



Leave a reply



Submit