设计模式代码示例
代码
单例模式(线程)using System;
using System.Collections;
using System.Threading;
//
//
// Generated by StarUML(tm) C# Add-In
//
// @ Project : 设计模式
// @ File Name : LoadBalancer.cs
// @ Date : 2010/12/21
// @ Author :
//
//
namespace LoadBalancer{
public class LoadBalancer {
private static LoadBalancer balancer ;
private ArrayList servers = new ArrayList() ;
private Random random = new Random();
protected LoadBalancer()
{
servers.Add("Server1");
servers.Add("Server2");
servers.Add("Server3");
servers.Add("server4");
servers.Add("server5");
}
public static LoadBalancer GetLoadBalancer()
{
if ( balancer == null)
{
Mutex mutex = new Mutex();
mutex.WaitOne();
if (balancer == null)
{
balancer = new LoadBalancer();
}
mutex.Close();
}
return balancer;
}
public string Server
{
get
{
int r = random.Next(servers.Count);
return servers[r].ToString();
}
}
}
}