Design Patterns (.NET)- Singleton Method Part-3


  1. Please read the first article from https://www.dhirubhai.net/pulse/design-patterns-net-foysal-ahmed-sifat? and the second one from https://www.dhirubhai.net/pulse/design-patterns-net-part-2-foysal-ahmed-sifat . It will help to catch the theory easily.?
  2. Singleton Method

Singleton method allows only one instance to be created of a singleton class at a time and then provide global access through the application.

Visual Diagram:

No alt text provided for this image

As you can see in the above diagram, different clients (NewObject a, NewObject b and NewObject n) trying to get the singleton instance. Once the client gets the singleton instance then they can invoke the methods (Method 1, Method 2, and Method n) using the same instance

The Advantages of using the Singleton Design Pattern in C# are as follows.

  1. The first and most important advantage of using the singleton design pattern in C# is that it takes care of concurrent access to the shared resource. That means if we are sharing a resource with multiple clients simultaneously, then concurrent access to that resource is well managed by the singleton design pattern.
  2. It can be lazy-loaded and also has Static Initialization.
  3. To share common data i.e. master data and configuration data which is not changed that frequently in an application. In that case, we need to cache the objects in memory.
  4. As it provides a single global point of access to a particular instance, so it is easy to maintain.
  5. To reduce the overhead of instantiating a heavy object again and again.

The following are the implementation guidelines for using the singleton design pattern in C#.

  1. You need to declare a constructor that should be private and parameterless. This is required because it is not allowed the class to be instantiated from outside the class. It only instantiates from within the class.
  2. The class should be declared as sealed which will ensure that it cannot be inherited. This is going to be useful when you are dealing with the nested class. We will discuss this scenario with an example in our upcoming article.
  3. You need to create a private static variable that is going to hold a reference to the single created instance of the class if any.
  4. You also need to create a public static property/method which will return the single-created instance of the singleton class. This method or property first check if an instance of the singleton class is available or not. If the singleton instance is available, then it returns that singleton instance otherwise it will create an instance and then return that instance.

Example of the Singleton Design Pattern using C#

Let us understand the Singleton Design pattern in C# with an example. There are many ways, we can implement the Singleton Design Pattern in C# are as follows.

  1. No Thread-Safe Singleton design pattern.
  2. Thread-Safety Singleton implementation.
  3. The Thread-Safety Singleton Design pattern implementation using Double-Check Locking.
  4. Thread-Safe Singleton Design pattern implementation without using the locks and no lazy instantiation.
  5. Fully lazy instantiation of the singleton class.
  6. Using .NET 4’s Lazy<T> type.


Implementational Example:

namespace SingletonDem

{

????public sealed class Singleton

????{

????????private static int counter = 0;

????????private static Singleton instance = null;

????????public static Singleton GetInstance

????????{

????????????get

????????????{

????????????????if (instance == null)

????????????????????instance = new Singleton();

????????????????return instance;

????????????}

????????}

????????

????????private Singleton()

????????{

????????????counter++;

????????????Console.WriteLine("Counter Value " + counter.ToString());

????????}

????????public void PrintDetails(string message)

????????{

????????????Console.WriteLine(message);

????????}

????}

}

namespace SingletonDemo

{

????class Program

????{

????????static void Main(string[] args)

????????{

????????????Singleton fromTeachaer = Singleton.GetInstance;

????????????fromTeachaer.PrintDetails("From Teacher");

????????????Singleton fromStudent = Singleton.GetInstance;

????????????fromStudent.PrintDetails("From Student");

????????????Console.ReadLine();

????????}

????}

}        

要查看或添加评论,请登录

Foysal Ahamed Sifat的更多文章

社区洞察

其他会员也浏览了