Hi Guys
Commenting out “if(instance == null)” is altering the otuput. Anyone knows please explain the reason.
Thank you
using System;
class Singleton
{
private static Singleton instance;
private static int numOfReference;
private string code;
private Singleton()
{
numOfReference = 0;
code = "Maasoom Faraz";
}
public static Singleton GetInstance()
{
//if (instance == null) Commenting out this will change the output
instance = new Singleton();
numOfReference++;
return instance;
}
public static int Reference
{
get { return numOfReference; }
}
public string Code
{
get { return code; }
set { code = value; }
}
}
public class SingletonClientCode
{
static void
{
//Singleton a = new Singleton();
//will result in error because constructor is private
Singleton b = Singleton.GetInstance();
Console.WriteLine(b.Code);
Console.WriteLine(Singleton.Reference);
Singleton c = Singleton.GetInstance();
Console.WriteLine(c.Code);
Console.WriteLine(Singleton.Reference);
c.Code = "Modified " + c.Code;
Singleton d = Singleton.GetInstance();
Console.WriteLine(d.Code);
Console.WriteLine(Singleton.Reference);
if (b == c && c == d && d == b)
Console.WriteLine("Instances are equal");
else
Console.WriteLine("Instances are not equal");
Console.ReadLine();
}
}
/*
Output
Maasoom Faraz
1
Maasoom Faraz
1
Maasoom Faraz
1
Instances are not equal
*/

AlanRank #669 · 3.6K reputationPosted Oct 7, 2007, 1:43 AM EDT
If you comment out that line, then every time the GeInstance() method is called a new Singleton object is created, which defeats the purpose of the singleton pattern.
If you leave it in, then the Singleton object is only created the first time GetInstance() is called. On subsequent calls, a reference is returned to the existing Singelton object, which is how it should be.
Posted Oct 7, 2007, 3:22 AM EDT
Thank you very much for the explanation, Alan
Posted Oct 8, 2007, 1:20 PM EDT
Singleton b = Singleton.GetInstance(); this code is quite new to me. I am unable to understand meaning of this code. Anyone knows please explain in detail.
AlanRank #669 · 3.6K reputationPosted Oct 8, 2007, 2:44 PM EDT
GetInstance() is a static method of the Singleton class and it therefore has to be preceded by the class name when called by outside code.
As the class is a singleton, GetInstance() has to be static because, if it were an instance method, then you'd need a reference to the single instance to call it in the first place :)
Posted Oct 8, 2007, 3:57 PM EDT
Thank you very much for explaining items in the RHS of the equal sign. It is much appreciated if you could explain items in the LHS as well.
Jan MontanoRank #1,295 · 1.3K reputationPosted Oct 8, 2007, 5:46 PM EDT
It's just an ordinary declaration of a class. Singleton.GetInstance() which returns a Singleton type must be put in a Singleton variable, in this case, b is our Singleton variable. Maybe you're confused with the class Singleton, it's a custom class, nothing special about it.
Suppose we have this...
class SomeClass
{
private static SomeClass instance;
private static int numOfReference;
private string code;
private SomeClass()
{
numOfReference = 0;
code = "Maasoom Faraz";
}
public static SomeClass GetInstance()
{
//if (instance == null) Commenting out this will change the output
instance = new SomeClass();
numOfReference++;
return instance;
}
public static int Reference
{
get { return numOfReference; }
}
public string Code
{
get { return code; }
set { code = value; }
}
}
We'll be using SomeClass b = SomeClass.GetInstance() instead.
Posted Oct 8, 2007, 6:35 PM EDT
Thank you very much for the explanation, Jan