单例模式

软件公司承接了一个服务器负载均衡(Load Balance)软件的开发工作,该软件运行在一台负载均衡服务器上,可以将并发访问和数据流量分发到服务器集群中的多台设备上进行并发处理,提高系统的整体处理能力,缩短响应时间。由于集群中的服务器需要动态删减,且客户端请求需要统一分发,因此需要确保负载均衡器的唯一性,只能有一个负载均衡器来负责服务器的管理和请求的分发,否则将会带来服务器状态的不一致以及请求分配冲突等问题。如何确保负载均衡器的唯一性是该软件成功的关键。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace LoadBalancer
{
    //负载均衡器LoadBalancer: 单例类, 真实环境下该类将非常复杂, 包括大量初始化的工作和业务方法,考虑到
    //代码的可读性和易理解性, 只列出部分与模式相关的核心代码
    class LoadBalancer
    {
        //私有静态成员变量, 存唯一实例
        private static LoadBalancer instance = null;
        //服务器集合
        private List<String> serverList = null;
//        private ArrayList testarray = new ArrayList();
        //私有构造函数
        private LoadBalancer()
        {
            serverList = new List<string>();
        }

        //公有静态成员方法, 返回唯一实例
        public static LoadBalancer getLoadBalancer()
        {
            if (instance == null)
            {
                instance = new LoadBalancer();
            }
            return instance;
        }

        //增加服务器
        public void addServer(String server)
        {
            serverList.Add(server);
        }

        //删除服务器
        public void removeServer(String server)
        {
            serverList.Remove(server);
        }

        //使用random类随机获取服务器
        public String getServer()
        {
            Random random = new Random();
            int i = random.Next(0, serverList.Count);
            return serverList[i];
        }
    }
/*
 * 饿汉式单例实现 
 * classs EagerSingleton
 * {
 *    private static final EagerSingleton instance = new EagerSingleton();
 *    private EagerSingleton(){}
 *    
 *    public static EagerSingleton getInstace(){
 *       return instance;
 *    }
 * }
    */

    /*
     * 懒汉式单例实现 延时加载技术 但是要处理线程锁
     * class LazySingleton
     * {
     *     private static LazySingleton instance = null;
     *     private LazySingleton(){}
     *     
     *     synchronized public static LazySingleton getInstance()
     *     {
     *        if (instance == null)
     *        {
     *           instance = new LazySingleton();
     *        }
     *        return instance;
     *     }
     * }
     * 
     * 该懒汉式单例类在 getInstance() 方法前增加 线程锁 处理多线程访问的问题 但是 每次调用getInstance时
     * 都需要进行线程锁判断, 在多线程的高并发访问环境中,将会导致系统性能大大降低, 所以进行一下改动
     * private volatile static LazySingleton instance = null;
     * public static LazySingleton getInstance()
     * {
     *    //第一重判断
     *    if (instance == null)
     *    {
     *       synchronized(LazySingleton.class)  此处用双重锁定处理
     *       {
     *          //第二重判断
     *          if (instance == null)
     *          {
     *              instance = new LazySingleton();
     *          }        
     *       }
     *    }
     *    return instance;
     * }
     * */

    /*
     * IoDH技术 处理单例类
     * 通过IoDH, 我们既可以实现延迟加载, 又可以保证线程安全, 不影响系统性能, 不失为一种最好的实现方式
     * 其缺点是与编程语言本身的特性有关, 很多面向对象语言不支持IoDH
     * 
     * class Singleton
     * {
     *   private Singleton()
     *   {}
     *   
     *   private static class HolderClass
     *   {
     *      private final static Singleton instance = new Singleton();
     *   }
     *   
     *   public static Singleton getInstance()
     *   {
     *      return HolderClass.instance;
     *   }
     * }
     * 
     * */

    class Program
    {
        static void Main(string[] args)
        {
            LoadBalancer balancer1, balancer2, balancer3, balancer4;
            balancer1 = LoadBalancer.getLoadBalancer();
            balancer2 = LoadBalancer.getLoadBalancer();
            balancer3 = LoadBalancer.getLoadBalancer();
            balancer4 = LoadBalancer.getLoadBalancer();

            if (balancer1 == balancer2 && balancer2 == balancer3 && balancer4 == balancer3)
            {
                Console.WriteLine("服务器负载均衡器局具有唯一性");
            }

            // 增加服务器
            balancer1.addServer("server 1");
            balancer1.addServer("server 2");
            balancer1.addServer("server 3");
            balancer1.addServer("server 4");

            //模拟客户端请求的分发
            for (int i = 0; i < 20; i++)
            {
                String server = balancer1.getServer();
                Console.WriteLine("分发请求到服务起" + server);
            }
        }
    }
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值