Tuesday, March 31, 2009

Design Patterns :: Singleton

Design Patterns :: Singleton

There are many common situations when singleton pattern is used:
 - Accesing resources in shared mode
 - Configuration Classes
 - Logger Classes
 - Serialization
 - Network Port Interface

Singleton Design Pattern
Singleton Design Pattern

Check list
1.Define a private static attribute in the "single instance" class.
2.Define a public static accessor function in the class.
3.Do "lazy initialization" (creation on first use) in the accessor function.
4.Define all constructors to be protected or private.
5.Clients may only use the accessor function to manipulate the Singleton.



  1. using System;

  2. namespace DesignPatternsSingleton
  3. {
  4.   /// <summary>
  5.   /// MainApp startup class for Design Patterns :: Singleton
  6.   /// </summary>
  7.   class MainApp
  8.   {
  9.   /// <summary>
  10.   /// The Singleton class.
  11.   /// </summary>
  12.   public class Singleton
  13.   {
  14.     private static Singleton _instance;

  15.     // Constructor is 'protected'
  16.     protected Singleton()
  17.     {
  18.     }

  19.     public static Singleton Instance()
  20.     {
  21.       // Uses lazy initialization.
  22.       // Note: this is not thread safe.
  23.       if (_instance == null)
  24.       {
  25.         _instance = new Singleton();
  26.       }

  27.       return _instance;
  28.     }
  29.   }
  30. }

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Threading;

  4. namespace DesignPatternsSingleton
  5. {
  6.   /// <summary>
  7.   /// MainApp startup class for Real-World
  8.   /// Singleton Design Pattern.
  9.   /// </summary>
  10.   class MainApp
  11.   {
  12.     /// <summary>
  13.     /// Entry point into console application.
  14.     /// </summary>
  15.     static void Main()
  16.     {
  17.       LoadBalancer b1 = LoadBalancer.GetLoadBalancer();
  18.       LoadBalancer b2 = LoadBalancer.GetLoadBalancer();
  19.       LoadBalancer b3 = LoadBalancer.GetLoadBalancer();
  20.       LoadBalancer b4 = LoadBalancer.GetLoadBalancer();

  21.       // Same instance?
  22.       if (b1 == b2 && b2 == b3 && b3 == b4)
  23.       {
  24.         Console.WriteLine("Same instance\n");
  25.       }

  26.       // Load balance 15 server requests
  27.       LoadBalancer balancer = LoadBalancer.GetLoadBalancer();
  28.       for (int i = 0; i < 15; i++)
  29.       {
  30.         string server = balancer.Server;
  31.         Console.WriteLine("Dispatch Request to: " + server);
  32.       }

  33.       // Wait for user
  34.       Console.ReadKey();
  35.     }
  36.   }

  37.   /// <summary>
  38.   /// The 'Singleton' class
  39.   /// </summary>
  40.   class LoadBalancer
  41.   {
  42.     private static LoadBalancer _instance;
  43.     private List<string> _servers = new List<string>();
  44.     private Random _random = new Random();

  45.     // Lock synchronization object
  46.     private static object syncLock = new object();

  47.     // Constructor (protected)
  48.     protected LoadBalancer()
  49.     {
  50.       // List of available servers
  51.       _servers.Add("ServerI");
  52.       _servers.Add("ServerII");
  53.       _servers.Add("ServerIII");
  54.       _servers.Add("ServerIV");
  55.       _servers.Add("ServerV");
  56.     }

  57.     public static LoadBalancer GetLoadBalancer()
  58.     {
  59.       // Support multithreaded applications through
  60.       // 'Double checked locking' pattern which (once
  61.       // the instance exists) avoids locking each
  62.       // time the method is invoked
  63.       if (_instance == null)
  64.       {
  65.         lock (syncLock)
  66.         {
  67.           if (_instance == null)
  68.           {
  69.             _instance = new LoadBalancer();
  70.           }
  71.         }
  72.       }

  73.       return _instance;
  74.     }

  75.     // Simple, but effective random load balancer
  76.     public string Server
  77.     {
  78.       get
  79.       {
  80.         int r = _random.Next(_servers.Count);
  81.         return _servers[r].ToString();
  82.       }
  83.     }
  84.   }
  85. }

No comments:

Post a Comment