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

來源:互聯網
上載者:User

意圖

保證一個類只有一個執行個體,並提供訪問它的全域訪問點。

情境

我們現在要做一個網路遊戲的服務端程式,需要考慮怎麼樣才能承載大量的使用者。在做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);

        }

    }

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.