public static SingleObject getInstance() { if (instance == null) { instance = new SingleObject(); } return(instance); }
static void Main(string[] args) { //illegal construct //Compile Time Error: The constructor SingleObject() is not visible // SingleObject object = new SingleObject(); //Get the only object available SingleObject obj = SingleObject.GetInstance(); //show the message obj.ShowMessage("Singleton Pattern Demo..."); }
static void Main(string[] args) { SingleObject ob = SingleObject.getInstance(); ob.setValor("Objeto 1"); ob.ShowMethod(); SingleObject ob2 = SingleObject.getInstance(); ob2.ShowMethod(); SingleObject ob3 = SingleObject.getInstance(); ob2.ShowMethod(); Console.ReadLine(); }