Skip to content

Instantly share code, notes, and snippets.

@yemrekeskin
Created March 28, 2016 17:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yemrekeskin/dbfa6895115c42e9e09f to your computer and use it in GitHub Desktop.
Save yemrekeskin/dbfa6895115c42e9e09f to your computer and use it in GitHub Desktop.
namespace Singleton.Sample1
{
class Program
{
static void Main(string[] args)
{
var myFirstObject = Singleton.Instance();
var mySecondObject = Singleton.Instance();
var myThirdObject = Singleton.Instance();
//var myFourthObject = new Singleton();
if(myFirstObject == mySecondObject)
Console.WriteLine("Nesneler aynı");
if(mySecondObject != myThirdObject)
Console.WriteLine("Nesneler aynı değil");
Console.ReadLine();
}
}
class Singleton
{
private static Singleton _instance = null;
// yapıcı methodun erişim belirleyicisi 'protected'
// private da kullanabilirdir.
protected Singleton()
{
Console.WriteLine("Nesne oluşturuluyor...");
}
public static Singleton Instance()
{
// Bu kod thread-safe bir kod değil
if (_instance == null)
_instance = new Singleton();
return _instance;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment