/// <summary>
/// A simple singleton class implements.
/// </summary>
public sealed class Singleton
{
private static Singleton _instance = null;
/// <summary>
/// Prevents a default instance of the
/// <see cref="Singleton"/> class from being created.
/// </summary>
private Singleton()
{
}
/// <summary>
/// Gets the instance.
/// </summary>
public static Singleton Instance
{
get { return _instance ?? (_instance = new Singleton()); }
}
}
/// <summary>
/// A thread-safe singleton class.
/// </summary>
public sealed class Singleton
{
private static Singleton _instance = null;
private static readonly object SynObject = new object();
Singleton()
{
}
/// <summary>
/// Gets the instance.
/// </summary>
public static Singleton Instance
{
get
{
// Syn operation.
lock (SynObject)
{
return _instance ?? (_instance = new Singleton());
}
}
}
}
/// <summary>
/// Double-Checked Locking implements a thread-safe singleton class
/// </summary>
public sealed class Singleton
{
private static Singleton _instance = null;
// Creates an syn object.
private static readonly object SynObject = new object();
Singleton()
{
}
public static Singleton Instance
{
get
{
// Double-Checked Locking
if (null == _instance)
{
lock (SynObject)
{
if (null == _instance)
{
_instance = new Singleton();
}
}
}
return _instance;
}
}
}
/// <summary>
/// Defines a test class.
/// </summary>
class Test
{
public static string x = EchoAndReturn("In type initializer");
public static string EchoAndReturn(string s)
{
Console.WriteLine(s);
return s;
}
}
class Test
{
public static string x = EchoAndReturn("In type initializer");
// Defines a parameterless constructor.
static Test()
{
}
public static string EchoAndReturn(string s)
{
Console.WriteLine(s);
return s;
}
}
class Test
{
public static string x = EchoAndReturn("In type initializer");
static Test()
{
}
public static string EchoAndReturn(string s)
{
Console.WriteLine(s);
return s;
}
}
class Driver
{
public static void Main()
{
Console.WriteLine("Starting Main");
// Invoke a static method on Test
Test.EchoAndReturn("Echo!");
Console.WriteLine("After echo");
Console.ReadLine();
// The output result:
// Starting Main
// In type initializer
// Echo!
// After echo
}
}
public static void Main()
{
Console.WriteLine("Starting Main");
// Invoke a static method on Test
Test.EchoAndReturn("Echo!");
Console.WriteLine("After echo");
//Reference a static field in Test
string y = Test.x;
//Use the value just to avoid compiler cleverness
if (y != null)
{
Console.WriteLine("After field access");
}
Console.ReadKey();
// The output result:
// In type initializer
// Starting Main
// Echo!
// After echo
// After field access
}
class Test
{
public static string x = EchoAndReturn("In type initializer");
static Test()
{
}
public static string EchoAndReturn(string s)
{
Console.WriteLine(s);
return s;
}
}
public sealed class Singleton
{
private static readonly Singleton _instance = new Singleton();
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Singleton()
{
}
/// <summary>
/// Prevents a default instance of the
/// <see cref="Singleton"/> class from being created.
/// </summary>
private Singleton()
{
}
/// <summary>
/// Gets the instance.
/// </summary>
public static Singleton Instance
{
get
{
return _instance;
}
}
}
/// <summary>
/// Delaies initialization.
/// </summary>
public sealed class Singleton
{
private Singleton()
{
}
/// <summary>
/// Gets the instance.
/// </summary>
public static Singleton Instance { get { return Nested._instance; } }
private class Nested
{
// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static Nested()
{
}
internal static readonly Singleton _instance = new Singleton();
}
}
/// <summary>
/// .NET 4's Lazy<T> type
/// </summary>
public sealed class Singleton
{
private static readonly Lazy<Singleton> lazy =
new Lazy<Singleton>(() => new Singleton());
public static Singleton Instance { get { return lazy.Value; } }
private Singleton()
{
}
}
/// <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; }
}
/// <summary>
/// The 'Singleton' class
/// </summary>
sealed class LoadBalancer
{
private static readonly LoadBalancer _instance =
new LoadBalancer();
// Type-safe generic list of servers
private List<Server> _servers;
private Random _random = new Random();
static LoadBalancer()
{
}
// Note: constructor is 'private'
private LoadBalancer()
{
// Load list of available servers
_servers = new List<Server>
{
new Server{ Name = "ServerI", IP = "192.168.0.108" },
new Server{ Name = "ServerII", IP = "192.168.0.109" },
new Server{ Name = "ServerIII", IP = "192.168.0.110" },
new Server{ Name = "ServerIV", IP = "192.168.0.111" },
new Server{ Name = "ServerV", IP = "192.168.0.112" },
};
}
/// <summary>
/// Gets the instance through static initialization.
/// </summary>
public static LoadBalancer Instance
{
get { return _instance; }
}
// Simple, but effective load balancer
public Server NextServer
{
get
{
int r = _random.Next(_servers.Count);
return _servers[r];
}
}
}
static void Main()
{
LoadBalancer b1 = LoadBalancer.Instance;
b1.GetHashCode();
LoadBalancer b2 = LoadBalancer.Instance;
LoadBalancer b3 = LoadBalancer.Instance;
LoadBalancer b4 = LoadBalancer.Instance;
// 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.Instance;
for (int i = 0; i < 15; i++)
{
string serverName = balancer.NextServer.Name;
Console.WriteLine("Dispatch request to: " + serverName);
}
Console.ReadKey();
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有