-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
56 lines (44 loc) · 1.72 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SingletonPattern
{
sealed class SocketManager
{
public DateTime CreatedAt { get; private set; }
public SocketManager()
{
CreatedAt = DateTime.Now;
}
/// <summary>
/// Bu degisken sayesinde uygulama acik oldugu surece SocketManager hayatta kalacak ve bir daha (hata yapılmadığı sürece) yeni bir ornegi yaratılmayacak.
/// </summary>
private static SocketManager activeConnection;
public static SocketManager GetSocketManager()
{
//eger daha once connection yaratilmamis ise yeni bir connection yaratiyoruz.
if (activeConnection == null)
activeConnection = new SocketManager();
//yeni yaratilan veya zaten var olan, sonuc olarak tek bir tane olan active connection'i return ediyoruz.
return activeConnection;
}
}
class Program
{
static void Main(string[] args)
{
while (true)
{
// dongu icerisinde surekli olarak getsocketmanager kullanıyorum, geri donen socket manager ilk defasında yaaratılan oluyor.
//Bu gibi durumlarda Singleton kullanmak ileride developerlarin hata yapmasına engel oluyor.
SocketManager sm = SocketManager.GetSocketManager();
Console.WriteLine("Enter to see the time that Socket manager has been created");
Console.ReadLine();
Console.WriteLine("Date:{0}\n", sm.CreatedAt);
Console.WriteLine("Press CTRL+C to exit");
}
}
}
}