No nonsense C # design Pattern II: Singleton

Source: Internet
Author: User

Intention

Guarantees that a class has only one instance and provides access to its global access point.

Scene

We now want to do a Web game server-side program, we need to consider how to host a large number of users. When doing web programs have a variety of load balancing scheme, whether through the hardware or software implementation, the basic idea is to have a unified portal, and then by it to assign users to each server up.

The problem to consider is that even in a multithreaded concurrency state, users can only be allocated through a single portal, thereby introducing the singleton pattern to achieve this unique portal.

Sample code

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





        }





    }





}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.