Created
February 2, 2016 11:08
-
-
Save sudipto80/7d65399d6b59ed488df8 to your computer and use it in GitHub Desktop.
Singleton2
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
using System; | |
using System.Collections.Generic; | |
namespace DoFactory.GangOfFour.Singleton.NETOptimized | |
{ | |
class MainApp | |
{ | |
static void Main() | |
{ | |
} | |
} | |
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() | |
{ | |
} | |
public static LoadBalancer GetLoadBalancer() | |
{ | |
return _instance; | |
} | |
// Simple, but effective load balancer | |
public Server NextServer | |
{ | |
} | |
} | |
/// <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; } | |
private static Server _instance; | |
private Server() | |
{ | |
} | |
public static Server Instance | |
{ | |
get | |
{ | |
return new Server(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment