Sunday, December 27, 2015

C# : Singleton Vs Static Class


A singleton object stores common data in only one place and in similar a static class is also used to store single-instance data so by using singleton or static class we can able to save state between usages and store cache data to improve performance.

Singleton is an object which can only be instantiated once and shared.

C# Singleton Pattern

The big difference between a singleton and a bunch of static methods is that singletons can implement interfaces (or derive from useful base classes, although that's less common IME), so you can pass around the singleton as if it were "just another" implementation.

Other differences as below:
  • Singleton object stores in Heap but, static object stores in stack
  • We can clone the object of Singleton but, we cannot clone the static class object
  • Singleton class follow the OOP(object oriented principles) but not static class
  • We can implement interface with Singleton class but not with Static class.
The Singleton pattern has several advantages over static classes. 

·         A singleton can extend classes and implement interfaces, while a static class cannot (it can extend classes, but it does not inherit their instance members). 

·         A singleton can be initialized lazily or asynchronously while a static class is generally initialized when it is first loaded, leading to potential class loader issues. 

However the most important advantage, though, is that singletons can be handled polymorphic ally without forcing their users to assume that there is only one instance.

Singleton Pattern: 

Here is an example of singleton pattern implemented class HttpClientManager, which manages the single instance of HttpClient, if there is no any instance of HttpClient, it creates new object of HttpClient.

C# Code:
public sealed class HttpClientManager
    {
        private static HttpClient instance;
        private static object syncRoot = new object();
        public static HttpClient Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            var handler = new HttpClientHandler {
UseDefaultCredentials = true,
Credentials = CredentialCache.DefaultCredentials };
                            var client = new HttpClient(handler, false)
                            {
                                BaseAddress = new Uri("http://localhost")
                            };
                            instance = client;
                        }
                    }
                }
                return instance;
            }
        }

    }
 


Thanks for visiting!!


No comments:

SQL Server - Identify unused indexes

 In this blog, we learn about the index usage information (SYS.DM_DB_INDEX_USAGE_STATS) and analyze the index usage data (USER_SEEKS, USER_S...