Design Mode: Singleton mode and Design Mode

Source: Internet
Author: User

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

}

}

}

 

Related Article

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.