Step by Step – Singleton Pattern

Ravyii
Posted by in Design Pattern & Practices category on for Beginner level | Points: 250 | Views : 8321 red flag
Rating: 4.5 out of 5  
 2 vote(s)

Exploring the usage of singleton pattern and understanding the usage of static vs singleton..

Introduction

In this article I would like to share my understanding on singleton pattern and hope its useful to beginners.

Objective

Understanding singleton and its usage.

Using the code

In this article I want to address the following questions

  1. What is singleton?
  2. Why we need singleton?
  3. How to create a Singleton class?
  4. When to use singleton?

Ok. Now let’s face our first question.

1. What is singleton?

Singleton is a design pattern that falls into creational pattern category. This pattern focuses on creating an object only once. So the ground rule is at any point of time in the application runtime only one instance of a class will be created and used across the application.

That’s simple, now why we need it? don’t we have any other alternative? let’s explore…

2. Why we need singleton?

It’s a amazing question!. Can’t we achieve without this pattern?

To my knowledge I think creating a class as static can solve this as static class doesn’t allow to create an instance. When we create a class as static it forces you to define all your methods as static. And more over in case of singleton we can pass the instance to other methods and your code is well organized.

3. How to create a Singleton class?

In order to create a class as singleton we need to address the following things.

First thing we should restrict the class being instantiated. Let’s make the constructor of the class as “Private” so we can’t instantiate from other classes.

public class Singleton

  private Singleton() { }
}

Ok, now we have restricted then how to make this class accessible to do our job? In order to achieve this expose a public static property through which the Instance of the Singleton class will be available.


public class Singleton
{
private static readonly Singleton Instance = newSingleton();
public static Singleton GetInstance
{
   get { return Instance; }
}

private Singleton()
{
}
}

Here if you notice we have made the variable “Instance” as static read-only which is because it forces you to initialize either at variable declaration or at static constructor level.

Thus we have achieved implementing our class using singleton pattern. Now we need to think when this will be useful to answer our last question.

4. When to use singleton?

There are lots of places where this pattern will be helpful to name some of them, we can implement when implementing logger class or DatabaseConnection class or in thread model application etc.


Conclusion

I hope this article opens up a initial spark to explore Singleton pattern. Only one ground rule we need to consider "Only single instance" at any point of time.

Page copy protected against web site content infringement by Copyscape

About the Author

Ravyii
Full Name: Ravishankar Balasundaram
Member Level: Starter
Member Status: Member
Member Since: 3/1/2012 4:35:03 AM
Country: India
ravyii
http://www.dotnetfunda.com
I am ravishankar, passionate towards Microsoft Technologies. I am interested in Architecture design and SOA Based solution implementation.

Login to vote for this post.

Comments or Responses

Posted by: Sheonarayan on: 3/1/2012 | Points: 25
Good effort, keep it up Ravishankar !

Thanks
Posted by: Akiii on: 3/4/2012 | Points: 25
Good article. Please do give more articles on Singleton Design Pattern.


Thanks and Regards
Akiii
Posted by: muralikrishnasurap-12704 on: 4/10/2012 | Points: 25
Good one...

Login to post response

Comment using Facebook(Author doesn't get notification)