Singleton Design Pattern in C#

The Pattern Itself

The Singleton design pattern is a creational type of design pattern. We have distinct categories of design patterns, out of which creational is the one which deals with instance creation and initialization. This pattern helps a programmer to write a program with more flexibility of creating objects subjective to various cases, the example of which are Singleton, Factory, Abstract factory etc. Covering design pattern types are out of the scope of this article, so let’s focus on Singleton design pattern.

A Singleton design pattern enables a developer to write code where only one instance of a class is created and the threads or other processes should refer to that single instance of the class. We, at a certain point in time, might be in a situation where we need to write a code that tells we only need one instance of a class and if some other class tries to create an object of that class, then the already instantiated object is shared to that class. One very common and suitable example is a log writer class. We might have a situation where we must maintain a single file for writing logs for requests coming to our .NET application from various clients like a mobile client, web client or any windows application client. In this case, there should be a class which takes this responsibility of writing the logs in a single file.

Since the requests come from multiple clients simultaneously, there should be a mechanism where only one request is logged at a time and on the other hand other requests should not be missed, instead should be taken care diligently to be logged, so that those requests when logged, do not override or conflict with the already logged requests. To achieve this, we can make a class singleton by following singleton pattern guidelines where a single thread-safe object is shared between all the requests which we’ll discuss in detail in this article with practical examples.

Advantages of Singleton

Let’s highlight the advantages of singleton class or pattern first before we jump into actual implementation. The first advantage that we can make out from the request logging example is that singleton takes care of concurrent access to a shared resource, which means if we are sharing a resource with multiple clients simultaneously, then it should be well taken care of. In our case, our log file is the shared resource and singleton makes sure, every client accesses it with no deadlock or conflict. The second advantage is that it only allows one instance of a class responsible for sharing the resource, which is shared across all the clients or applications in a controlled state.

Guidelines

Every pattern is based on certain guidelines which should be followed while implementing the pattern. These guidelines help us build a robust pattern and each guideline has its significance in the pattern that we’ll discuss which creating a singleton class. Stick to the following guidelines whenever you need to implement Singleton design pattern in C#.

  1. Check that the implementation when done, creates only one instance of the class, and there should be only one point from where an instance is to be created.
  2. The singleton’s class constructors should be private so that no class can directly instantiate the singleton class.
  3. There should be a static property/method that takes care of singleton class instantiation and that property should be shared across applications and is solely responsible for returning a singleton instance.
  4. The C# singleton class should be sealed so that it could not be inherited by any other class, this is useful when we deal with nested class structure. We’ll discuss this scenario as well later when we implement the singleton.

Basic Singleton Implementation

That’s a lot of theory covered, now let’s practically implement the singleton pattern. We’ll cover this step by step. Read more...

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

Akhil Mittal的更多文章

社区洞察

其他会员也浏览了