设计模式之单例模式

1、类图

实例图

2、创建项目

………………………………

3、 新建周报类LoadBalancer:负载均衡器类,充当单例角色,在真实环境下该类非常复杂,包括大量初始化的工作和业务方法,考虑到代码的可读性和易理解性,在此只列出部分与模式相关的核心代码。

using System;

using System.Collections;

using System.Threading;

namespace SingletonSample

{

    class LoadBalancer

    {

        //私有静态成员变量,存储唯一实例

        private static LoadBalancer instance = null;

        //服务器集合

        private ArrayList serverList = null;

 

        //私有构造函数

        private LoadBalancer()

        {

            serverList = new ArrayList();

        }

        //公有静态成员方法,返回唯一实例

        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();

            Thread.Sleep(10); //适度的线程等待可以保证随机数生成的不是上一次的结果

            int i = random.Next(serverList.Count);

            return serverList[i].ToString();

        }

    }

}

4、 新建Singleton类:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace SingletonSample

{

    public class Singleton

    {

        private static Singleton _singleton = null; //定义私有的静态成员变量——唯一的实例对象

 

        /// <summary>

        /// 定义私有的构造函数,防止被外界实例化

        /// </summary>

        private Singleton()

        {

        }

        /// <summary>

        /// 定义公开的唯一的方位(工厂)方法,返回系统的唯一实例,实现自己创建自己

        /// </summary>

        /// <returns></returns>

        public static Singleton GetInstance()

        {

            if (_singleton == null)

                _singleton = new Singleton(); //如果为null,则实例化自己

            return _singleton;

        }

    }

}

 

5、 Program:客户端测试类

using System;

namespace SingletonSample

{

    class Program

    {

        static void Main(string[] args)

        {

            #region 测试单例,比较是否是同一个实例

            Singleton singleton1 = Singleton.GetInstance(); //创建单例对象1

            Singleton singleton2 = Singleton.GetInstance(); //创建单例对象1

            if (singleton1 == singleton2)

            {

                Console.WriteLine("singleton1与singleton2为:同一实例。");

            }

            else

            {

                Console.WriteLine("singleton1与singleton2为:不是同一实例。");

            }

            Console.Read();

            #endregion

            #region 案例

            //创建四个LoadBalancer对象

       LoadBalancer balancer1,balancer2,balancer3,balancer4;

        balancer1 = LoadBalancer.GetLoadBalancer();

        balancer2 = LoadBalancer.GetLoadBalancer();

        balancer3 = LoadBalancer.GetLoadBalancer();

        balancer4 = LoadBalancer.GetLoadBalancer();

    //判断服务器负载均衡器是否相同

    if (balancer1 == balancer2 && balancer2 == balancer3 && balancer3 == balancer4)

            {

    Console.WriteLine("服务器负载均衡器具有唯一性!");

    }

    //增加服务器

    balancer1.AddServer("Server 1");

    balancer1.AddServer("Server 2");

    balancer1.AddServer("Server 3");

    balancer1.AddServer("Server 4"); 

            //模拟客户端请求的分发,如果输出结果全为同一个server,可以将i适当放大,例如改为"i < 100"

           int num = 10;

            for (int i = 0; i < num; i++)

            {

                string server = balancer1.GetServer();

                Console.WriteLine("分发请求至服务器: " + server);

            }

            Console.Read();

            #endregion

        }

    }

}

 

posted @ 2016-11-24 12:47  Jonny-Xhl  阅读(314)  评论(0编辑  收藏  举报