Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • Singleton Architecture

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 39
    Comment on it

    Singleton architecture as the name suggests will create a single instance of a class in the entire project.

     

    We will instantiate the class object only once in the entire project and will reuse it again and again no need to create another object of that class.

     

    But this architecture leads to the deadlock situation when its deployed on server and several clients are accessing your website or service

     

        /// <summary>
        /// The 'Singleton' class
        /// </summary>
        public class Singleton
        {
         // .NET guarantees thread safety for static initialization
         private static Singleton instance = null;
         private string Name{get;set;}
         private string IP{get;set;}
         private Singleton()
         {
         //To DO: Remove below line
         Console.WriteLine("Singleton Intance");
         
         Name = "Server1";
         IP = "192.168.1.23";
         }
         // Lock synchronization object
         private static object syncLock = new object();
         
         public static Singleton Instance
         {
         get
         {
         // Support multithreaded applications through
         // 'Double checked locking' pattern which (once
         // the instance exists) avoids locking each
         // time the method is invoked
         lock (syncLock)
         {
         if (Singleton.instance == null)
         Singleton.instance = new Singleton();
         
         return Singleton.instance;
         }
         }
         }
         
         public void Show()
         {
         Console.WriteLine("Server Information is : Name={0} & IP={1}", IP, Name);
         }
         
        }
         
        /// <summary>
        /// Singleton Pattern Demo
        /// </summary>
        /// 
        class Program
        {
         static void Main(string[] args)
         {
         Singleton.Instance.Show();
         Singleton.Instance.Show();
         
         Console.ReadKey();
         }
        }

    So for avoiding deadlocks locking needs to be done while making requests

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: