I'm just getting into design patterns and am not sure I fully understand
the
usefulness of the singleton. I know you can use it to ensure that you only
have one instance of a class, but why would you do that rather then just
using static methods on your class. Any input would be great.
This could be used as global variables.
Suppose you create a CGlobal class
Instead of copying the reference to every function you just instanciate the
CGlobal class wherever you need it and you have access to these global
defined variables. This way your code gets simplified because you do not
have to write additional code to pass on the CGlobal reference.
For example this is all you need to do:
private void ThisMethod() {
CGlobal Global=new CGlobal();
Global.InitialValue=true; // sets the global
variable
}
Static methods are mostly used when you wish to do some processing but you
do not need to have access to the class internal variables, so you might not
even want to instanciate the class. You could see this as global defined
procedures and functions declared outside the class when you look at C++.
For example you can use static method like this:
CGlobal Global=new CGlobal();
bool bTest=Global.StaticMethodTest("SomeTest"); // Like you would do
normally
or even shorter
bool bTest=CGlobal.StaticMethodTest("SomeTest"); // note, no NEW
used!
I hope this solves your question