程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> 無廢話C#設計模式之二:Singleton(1)

無廢話C#設計模式之二:Singleton(1)

編輯:關於C語言

保證一個類只有一個實例,並提供訪問它的全局訪問點。

場景

我們現在要做一個網絡游戲的服務端程序,需要考慮怎麼樣才能承載大量的用戶。在做WEB程序的時候有各種負載均衡的方案,不管是通過硬件實現還是軟件實現,基本的思想就是有一個統一的入口,然後由它來分配用戶到各個服務器上去。

需要考慮的問題是,即使在多線程的並發狀態下,用戶只能通過一個唯一的入口來分配,由此引入了Singleton模式來實現這個唯一的入口。

示例代碼

using System;

using System.Collections.Generic;

using System.Threading;

namespace SingletonExample

{

    class Program

    {

        static void Main(string[] args)

        {

            ParameterizedThreadStart ts = new ParameterizedThreadStart(EnterPlayer);

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

            {

                Thread t = new Thread(ts);

                t.Start("player" + i);

            }

            LoadBalanceServer.GetLoadBalanceServer().ShowServerInfo();

        }

        static void EnterPlayer(object playerName)

        {

            LoadBalanceServer lbs = LoadBalanceServer.GetLoadBalanceServer();

            lbs.GetLobbyServer().EnterPlayer(playerName.ToString());

        }

    }

    class LoadBalanceServer

    {

        private const int SERVER_COUNT = 3;

        private List<LobbyServer> serverList = new List<LobbyServer>();

        private static volatile LoadBalanceServer lbs;

        private static object syncLock = new object();

        private LoadBalanceServer()

        {

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

            {

                serverList.Add(new LobbyServer("LobbyServer" + i));

            }

        }

        public static LoadBalanceServer GetLoadBalanceServer()

        {

            if (lbs == null )

            {

                lock (syncLock)

                {

                    if (lbs == null )

                    {

                        Thread.Sleep(100);

                        lbs = new LoadBalanceServer();

                    }

                }

            }

            return lbs;

        }

        public LobbyServer GetLobbyServer()

        {

            LobbyServer ls = serverList[0];

            for (int i = 1; i < SERVER_COUNT; i++)

            {

                if (serverList[i].PlayerList.Count < ls.PlayerList.Count)

                    ls = serverList[i];

            }

            return ls;

        }

        public void ShowServerInfo()

        {

            foreach (LobbyServer ls in serverList)

            {

                Console.WriteLine("=================" + ls.ServerName + "=================");

                foreach (string player in ls.PlayerList)

                {

                    Console.WriteLine(player);

                }

            }

        }

    }

    class LobbyServer

    {

        private List<string> playerList = new List<string>();

        public List<string> PlayerList

        {

            get { return playerList; }

        }

        private string serverName;

        public string ServerName

        {

            get { return serverName; }

        }

        public LobbyServer(string serverName)

        {

            this.serverName = serverName;

        }

        public void EnterPlayer(string playerName)

        {

            playerList.Add(playerName);

        }

    }

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved