Intention
Ensure that a class has only one instance and provides global access points to it.
Scenario
Now we want to create an online game server.Program, You need to consider how to carry a large number of users. There are various load balancing solutions for web programs. Whether implemented through hardware or software, the basic idea is to have a unified entrance, then, the user is allocated to each server.
The problem that needs to be considered is that even in the multi-thread concurrency state, the user can only be allocated through a unique entry, and thus the singleton mode is introduced to implement this unique entry.
ExampleCode
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 );
}
}
}
The code execution result is as follows:
Code Description
L The loadbalanceserver class implements the singleton mode. That is to say, under any circumstances, only one loadbalanceserver class instance will appear.
L lobbyserver class indicates the lobby service. After a user enters the lobby, the lobbyserver class serves the hall service. Here, we only save the user list in the lobby service.
L The Singleton mode has many implementation methods. Here, the dual lock mode is used. For C #, the static initialization method may be the most concise and will not be demonstrated here.
L The getlobbyserver () method of the loadbalanceserver class is responsible for returning a lobbyserver object with the minimum pressure.
L when loadbalanceserver is instantiated, the thread is sleep to simulate high concurrency. This is not necessary in the formal code.
When to use
L from the code perspective, when you want the class to have only one instance.
L from the application perspective, you want to have a manager responsible for a specific task. In addition, only one person can be allocated for this task. If there are more than one person, it will be messy. For example, if there are two parts for creating and processing the serial number, will it be repeated?
Implementation points
L a singleton class that ensures that its instance is unique.
Notes
L do not abuse Singleton mode. Singleton should be introduced only when it is not an instance. Otherwise, the scalability of the program may be limited.