Created
March 28, 2016 17:40
-
-
Save yemrekeskin/dbfa6895115c42e9e09f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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