Skip to content

Instantly share code, notes, and snippets.

@sudipto80
Created February 2, 2016 11:08
Show Gist options
  • Save sudipto80/7d65399d6b59ed488df8 to your computer and use it in GitHub Desktop.
Save sudipto80/7d65399d6b59ed488df8 to your computer and use it in GitHub Desktop.
Singleton2
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