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
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.
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 |
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.
- using System;
- namespace DesignPatternsSingleton
- {
- /// <summary>
- /// MainApp startup class for Design Patterns :: Singleton
- /// </summary>
- class MainApp
- {
- /// <summary>
- /// The Singleton class.
- /// </summary>
- public 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;
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Threading;
- namespace DesignPatternsSingleton
- {
- /// <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();
- }
- }
- }
- }
No comments:
Post a Comment