using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestsWithConsoleApp
{
// Genelde, singleton'lar ilk ihtiyaçları olduğu zaman çağrılırlar
// (instantiation ilk ihtiyaç olunan zamanda gerçekleşir).
// Generally, singletons are called when they are needed
// instantiation occurs when they are needed first time.
// It's called lazy construction.
// Creating more than one instances can be a problem in terms
// of performance. In these circumstances, we use singleton pattern.
// How to ensure that one class has one and only instance?
// How to use this unique instance easily?
// With singleton, we can find solutions to these questions:
// How a class can control of it's intantiation?
// How to limit the amount of instantiations of given class?
// How to prevent other users to instantiate any other instances?
// Sırasıyla:
// Class'ın constructor'ı gizlenir (private)
// public static bir metot oluşturulur; bu
// metot class'ın instance'ını oluşturur
// Dışarıdan direk constructor kullanılarak bir
// initialization yapılamaz. Yani şu yapılamaz:
// SingletonClass singletonClass = new SingletonClass();
// Instance ancak bir metot üzerinden çağrılabilir,
// çünkü constructor'a direkt erişim yoktur.
// Bu pattern'de ana fikir, bir instance'dan
// daha fazlasının üretilmesinin önlenmesi ve
// üretilen bu tek instance'ın initialization'ının
// class içinde kontrol edilmesidir.
class Program
{
public class Singleton
{
private static Singleton SingletonObject = new Singleton();
public static int Number = 0;
private Singleton()
{
Number++;
Console.WriteLine("Constructor çalışıyor. Kaç kez çalıştı? :" + Number);
// Constructor'ın sadece 1 kez çalışmış olduğunu görmüş olduk.
}
// Burada görüldüğü üzere parametre almayan
// constructor'ımızı private yaptık.
// Artık Singleton class'ı, bu constructor'ı
// kullanarak instantiate edilemez.
// Yani şunu yapamayız: Singleton singleton = new Singleton();
public static Singleton ReturnSingletonObject()
{
return SingletonObject;
}
public void ShowSomething()
{
Console.WriteLine("Something");
}
}
static void Main()
{
var singleton1 = Singleton.ReturnSingletonObject();
var singleton2 = Singleton.ReturnSingletonObject();
var singleton3 = Singleton.ReturnSingletonObject();
// görünüşte üç değişken oluşturup kullanmış olsak da;
// aslında toplamda sadece 1 obje ürettik.
// constructor'ın 1 kez çalışmış olmasından bunu anlamış olduk.
// Bildiğimiz üzere bir class'tan instantiation işlemi
// yapılırken constructor çalışıyor.
// Singleton'da amacımız en fazla tek bir obje (
// instance of a class) üretmekti.
// Birden fazla objenin üretilmesi engellenmeliydi.
// (In software engineering, the singleton pattern is a
// software design pattern that restricts the instantiation
// of a class to one "single" instance. This is useful when
// exactly one object is needed to coordinate actions across the system.
// The term comes from the mathematical concept of a singleton.)
// Biz de bunu yaptık. Constructor sadece
// ilk initialization'da çalıştı (singleton1 için).
// singleton2 ve singleton3 için herhangi bir şekilde
// constructor'a gidilmedi/instance oluşturulmadı.
singleton1.ShowSomething();
singleton2.ShowSomething();
singleton3.ShowSomething();
Console.ReadKey();
// Singleton SingletonObject = new Singleton();
// Yukarıda da bahsettiğimiz gibi, parametresiz
// constructor'ı private yaptığımız için class'tan
// bu şekilde bir instantiation yapamayız.
// Singleton'ı anti-pattern olarak addeden developer'lar vardır.
// An anti-pattern is a common response to a
// recurring problem that is usually ineffective and risks
// being highly counterproductive.[1][2] The term, coined in 1995
// by Andrew Koenig,[3] was inspired by a book, Design Patterns,
// which highlights a number of design patterns in
// software development that its authors considered to be
// highly reliable and effective.
}
}
}
// Farklı şekilde singleton:
// örneklerin birinde metot, birinde property
// kullandığımıza dikkat: csharp'ta bu pattern kullanılırken
// instance oluşturma işlemi hem property kullanılarak,
// hem metot kullanılarak uygulanmaktadır, ikisinden birisi seçilebilir)
public class Singleton
{
private static Singleton _instance;
// initialization'u görüldüğü gibi yukarıdaki satırda yapmıyoruz.
// if statement içinde yapıyoruz;
// şayet daha öncesinde initialization yapılmamışsa tabi...
public int Number { get; set; }
private Singleton()
{
Number++;
Console.WriteLine("Constructor çalışıyor. Kaç kez çalıştı? :" + Number);
}
public static Singleton GetSingletonInstance
{
get
{
if (_instance == null)
{
_instance = new Singleton();
return _instance;
}
return _instance;
}
}
public void ShowSomething()
{
Console.WriteLine("Something");
}
}
static void Main()
{
var singleton1 = Singleton.GetSingletonInstance;
var singleton2 = Singleton.GetSingletonInstance;
var singleton3 = Singleton.GetSingletonInstance;
singleton1.ShowSomething();
singleton2.ShowSomething();
singleton3.ShowSomething();
Console.ReadKey();
}
Hiç yorum yok:
Yorum Gönder