w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Is it good programming pracitce to assign Singleton to variables across multiple classes

In my opinion there is nothing bad with it.

Generally you would want to maintain a pointer to the singleton, not to some object that it contains. By keeping a pointer to the object it contains you are adding a harder dependency and a requirement that that object doesn't change or changes in some defined way. If you have defined and documented that then it should be ok, but, usually the singleton should be able to destroy that object and create a new one as required so you may want to rethink keeping a reference to it.

Keeping a reference to the singleton itself is fine because that will never be destroyed.

Properties will point to the same address as property in Singleton


and after non-singleton class will be destroyed also properties that where pointing to singleton so Reference Count = Refrence count - 1.


© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.