Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
34 views6 pages

Singleton

The Singleton design pattern ensures that only one instance of a class is created and provides a global access point to that instance. It defines an Instance method that returns the unique instance. The Singleton class is responsible for creating and maintaining its own sole instance. Sample code demonstrates a Singleton class in C# that lazily initializes a single LoadBalancer instance to distribute requests across server instances.

Uploaded by

Khizar Abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views6 pages

Singleton

The Singleton design pattern ensures that only one instance of a class is created and provides a global access point to that instance. It defines an Instance method that returns the unique instance. The Singleton class is responsible for creating and maintaining its own sole instance. Sample code demonstrates a Singleton class in C# that lazily initializes a single LoadBalancer instance to distribute requests across server instances.

Uploaded by

Khizar Abbasi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Singleton Design Pattern

definition sample code in C#


UML diagram
participants

definition

Ensure a class has only one instance and provide a global point of access to it.

Frequency of use: medium high

return to top

UML class diagram

return to top

participants

The classes and/or objects participating in this pattern are:

 Singleton (LoadBalancer)
o defines an Instance operation that lets clients access its unique instance. Instance is a
class operation.
o responsible for creating and maintaining its own unique instance.

return to top

sample code in C#

This structural code demonstrates the Singleton pattern which assures only a single instance (the
singleton) of the class can be created.
Hide code

// Singleton pattern -- Structural example

using System;

namespace DoFactory.GangOfFour.Singleton.Structural

/// <summary>

/// MainApp startup class for Structural

/// Singleton Design Pattern.

/// </summary>

class MainApp

/// <summary>

/// Entry point into console application.

/// </summary>

static void Main()

// Constructor is protected -- cannot use new

Singleton s1 = Singleton.Instance();

Singleton s2 = Singleton.Instance();

// Test for same instance

if (s1 == s2)

Console.WriteLine("Objects are the same instance");

// Wait for user

Console.ReadKey();

/// <summary>
/// The 'Singleton' class

/// </summary>

class Singleton

private static Singleton _instance;

// Constructor is 'protected'

protected Singleton()

public static Singleton Instance()

// Uses lazy initialization.

// Note: this is not thread safe.

if (_instance == null)

_instance = new Singleton();

return _instance;

Output
Objects are the same instance

This real-world code demonstrates the Singleton pattern as a LoadBalancing object. Only a single
instance (the singleton) of the class can be created because servers may dynamically come on- or off-line
and every request must go throught the one object that has knowledge about the state of the (web) farm.
Hide code

// Singleton pattern -- Real World example

using System;

using System.Collections.Generic;

using System.Threading;

namespace DoFactory.GangOfFour.Singleton.RealWorld

/// <summary>

/// MainApp startup class for Real-World

/// Singleton Design Pattern.

/// </summary>

class MainApp

/// <summary>

/// Entry point into console application.

/// </summary>

static void Main()

LoadBalancer b1 = LoadBalancer.GetLoadBalancer();

LoadBalancer b2 = LoadBalancer.GetLoadBalancer();

LoadBalancer b3 = LoadBalancer.GetLoadBalancer();

LoadBalancer b4 = LoadBalancer.GetLoadBalancer();

// Same instance?

if (b1 == b2 && b2 == b3 && b3 == b4)

Console.WriteLine("Same instance\n");

// Load balance 15 server requests

LoadBalancer balancer = LoadBalancer.GetLoadBalancer();

for (int i = 0; i < 15; i++)


{

string server = balancer.Server;

Console.WriteLine("Dispatch Request to: " + server);

// Wait for user

Console.ReadKey();

/// <summary>

/// The 'Singleton' class

/// </summary>

class LoadBalancer

private static LoadBalancer _instance;

private List<string> _servers = new List<string>();

private Random _random = new Random();

// Lock synchronization object

private static object syncLock = new object();

// Constructor (protected)

protected LoadBalancer()

// List of available servers

_servers.Add("ServerI");

_servers.Add("ServerII");

_servers.Add("ServerIII");

_servers.Add("ServerIV");

_servers.Add("ServerV");

}
public static LoadBalancer GetLoadBalancer()

// Support multithreaded applications through

// 'Double checked locking' pattern which (once

// the instance exists) avoids locking each

// time the method is invoked

if (_instance == null)

lock (syncLock)

if (_instance == null)

_instance = new LoadBalancer();

return _instance;

// Simple, but effective random load balancer

public string Server

get

int r = _random.Next(_servers.Count);

return _servers[r].ToString();

You might also like