Design Mode: Singleton mode and Design Mode
1. Class Diagram
Instance Diagram
2. Create a project
....................................
3. Create a weekly report class LoadBalancer: A Load balancer class that acts as a singleton. In a real environment, this class is very complex, including a large number of initialization work and business methods. Considering the readability and comprehensibility of the Code, only some core Code related to the mode is listed here.
Using System;
Using System. Collections;
Using System. Threading;
Namespace SingletonSample
{
Class LoadBalancer
{
// Private Static member variable to store unique instances
Private static LoadBalancer instance = null;
// Server set
Private ArrayList serverList = null;
// Private constructor
Private LoadBalancer ()
{
ServerList = new ArrayList ();
}
// Public static member method, returns a unique instance
Public static LoadBalancer GetLoadBalancer ()
{
If (instance = null)
{
Instance = new LoadBalancer ();
}
Return instance;
}
// Add servers
Public void AddServer (string server)
{
ServerList. Add (server );
}
// Delete the server
Public void RemoveServer (string server)
{
ServerList. Remove (server );
}
// Use the Random class to randomly retrieve the server
Public string GetServer ()
{
Random random = new Random ();
Thread. Sleep (10); // a moderate Thread wait can ensure that the random number is not the last result.
Int I = random. Next (serverList. Count );
Return serverList [I]. ToString ();
}
}
}
4. Create a Singleton class:
Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Namespace SingletonSample
{
Public class Singleton
{
Private static Singleton _ singleton = null; // defines a private static member variable-a unique instance object
/// <Summary>
/// Define private constructor to prevent external instantiation
/// </Summary>
Private Singleton ()
{
}
/// <Summary>
/// Define the public unique orientation (factory) method, return to the unique instance of the system, and create your own
/// </Summary>
/// <Returns> </returns>
Public static Singleton GetInstance ()
{
If (_ singleton = null)
_ Singleton = new Singleton (); // if it is null, instantiate itself
Return _ singleton;
}
}
}
5. Program: client test class
Using System;
Namespace SingletonSample
{
Class Program
{
Static void Main (string [] args)
{
# Region test Singleton to check whether the same instance is used
Singleton singleton1 = Singleton. GetInstance (); // create a Singleton object 1
Singleton singleton2 = Singleton. GetInstance (); // create a Singleton object 1
If (singleton1 = singleton2)
{
Console. WriteLine ("singleton1 and singleton2 are the same instance. ");
}
Else
{
Console. WriteLine ("singleton1 and singleton2 are: not the same instance. ");
}
Console. Read ();
# Endregion
# Region case
// Create four LoadBalancer objects
LoadBalancer balancer1, balancer2, balancer3, balancer4;
Balancer1 = LoadBalancer. GetLoadBalancer ();
Balancer2 = LoadBalancer. GetLoadBalancer ();
Balancer3 = LoadBalancer. GetLoadBalancer ();
Balancer4 = LoadBalancer. GetLoadBalancer ();
// Determine whether the Server Load balancer is the same
If (balancer1 = balancer2 & balancer2 = balancer3 & balancer3 = balancer4)
{
Console. WriteLine ("Server Load balancer is unique! ");
}
// Add servers
Balancer1.AddServer ("Server 1 ");
Balancer1.AddServer ("Server 2 ");
Balancer1.AddServer ("Server 3 ");
Balancer1.AddServer ("Server 4 ");
// Simulate the distribution of client requests. If the output result is all the same server, you can enlarge the I as needed, for example, changing it to "I <100"
Int num = 10;
For (int I = 0; I <num; I ++)
{
String server = balancer1.GetServer ();
Console. WriteLine ("Distribute request to server:" + server );
}
Console. Read ();
# Endregion
}
}
}