-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathProgram.OptimizedCode.cs
122 lines (108 loc) · 3.34 KB
/
Program.OptimizedCode.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Collections.Generic;
/// <summary>
/// The.NET optimized code demonstrates the same code as above but uses more modern, built-in .NET features.
/// Here an elegant .NET specific solution is offered.The Singleton pattern simply uses a private constructor
/// and a static readonly instance variable that is lazily initialized.Thread safety is guaranteed by the compiler.
/// </summary>
namespace DoFactory.GangOfFour.Singleton.NETOptimized
{
/// <summary>
/// MainApp startup class for .NET optimized
/// Singleton Design Pattern.
/// </summary>
class MainApp
{
/// <summary>
/// Entry point into console application.
/// </summary>
static void Main()
{
LoadBalancer b1 = LoadBalancer.GetLoadBalancer();
LoadBalancer b2 = LoadBalancer.GetLoadBalancer();
LoadBalancer b3 = LoadBalancer.GetLoadBalancer();
LoadBalancer b4 = LoadBalancer.GetLoadBalancer();
// Confirm these are the same instance
if (b1 == b2 && b2 == b3 && b3 == b4)
{
Console.WriteLine("Same instance\n");
}
// Next, load balance 15 requests for a server
LoadBalancer balancer = LoadBalancer.GetLoadBalancer();
for (int i = 0; i < 15; i++)
{
string serverName = balancer.NextServer.Name;
Console.WriteLine("Dispatch request to: " + serverName);
}
// Wait for user
Console.ReadKey();
}
}
/// <summary>
/// The 'Singleton' class
/// </summary>
sealed class LoadBalancer
{
// Static members are 'eagerly initialized', that is,
// immediately when class is loaded for the first time.
// .NET guarantees thread safety for static initialization
private static readonly LoadBalancer _instance =
new LoadBalancer();
// Type-safe generic list of servers
private List<Server> _servers;
private Random _random = new Random();
// Note: constructor is 'private'
private LoadBalancer()
{
// Load list of available servers
_servers = new List<Server>
{
new Server{ Name = "ServerI", IP = "120.14.220.18" },
new Server{ Name = "ServerII", IP = "120.14.220.19" },
new Server{ Name = "ServerIII", IP = "120.14.220.20" },
new Server{ Name = "ServerIV", IP = "120.14.220.21" },
new Server{ Name = "ServerV", IP = "120.14.220.22" },
};
}
public static LoadBalancer GetLoadBalancer()
{
return _instance;
}
// Simple, but effective load balancer
public Server NextServer
{
get
{
int r = _random.Next(_servers.Count);
return _servers[r];
}
}
}
/// <summary>
/// Represents a server machine
/// </summary>
class Server
{
// Gets or sets server name
public string Name { get; set; }
// Gets or sets server IP address
public string IP { get; set; }
}
}
//Output
//Same instance
//ServerIV
//ServerIV
//ServerIII
//ServerV
//ServerII
//ServerV
//ServerII
//ServerII
//ServerI
//ServerIV
//ServerIV
//ServerII
//ServerI
//ServerV
//ServerIV