Topics
Published on May 13, 2017 by kudvenkat

Text version of the video
csharp-video-tutorials.blogspot.com/2017/05/singleton-design-pattern.html

Slides
csharp-video-tutorials.blogspot.com/2017/05/singleton-design-pattern_13.html

Design Patterns Tutorial

All Dot Net and SQL Server Tutorials in English
www.youtube.com/user/kudvenkat/playlists?view=1&sort=dd

All Dot Net and SQL Server Tutorials in Arabic
www.youtube.com/c/KudvenkatArabic/playlists

In this tutorial we will discuss
1. What is Singleton Design Pattern
2. Singleton as Creational Pattern
3. Implementation Guidelines
4. How do we implement a Singleton class

This is continuation to Part 1 of Design Patterns Tutorial. So please watch Part 1 before proceeding.

Singleton Pattern belongs to Creational type pattern. As discussed in our previous video, Gang of four have defined five design patterns that belongs to creational design type category. Singleton is one among them and the rest are Factory, Abstract Factory, Builder and Prototype patterns. As the name implies, creational design type deals with object creation mechanisms. Basically, to simplify this, creational pattern explain us the creation of objects in a manner suitable to a given situation.

Singleton design pattern is used when we need to ensure that only one object of a particular class is Instantiated. That single instance created is responsible to coordinate actions across the application.

Advantages and Guidelines for Singleton implementation.

Concurrent access to the resource is well managed by singleton design pattern.

As part of the Implementation guidelines we need to ensure that only one instance of the class exists by declaring all constructors of the class to be private. Also, to control the singleton access we need to provide a static property that returns a single instance of the object.

Singleton Class Implementation Example

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
// First version of Singleton demo
namespace SingletonDemo
{
class Program
{
static void Main(string[] args)
{
/*
* Assuming Singleton is created from employee class
* we refer to the GetInstance property from the Singleton class
*/
Singleton fromEmployee = Singleton.GetInstance;
fromEmployee.PrintDetails(“From Employee”);
/*
* Assuming Singleton is created from student class
* we refer to the GetInstance property from the Singleton class
*/
Singleton fromStudent = Singleton.GetInstance;
fromStudent.PrintDetails(“From Student”);

Console.ReadLine();
}
}
}

Singleton.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace SingletonDemo
{
/*
* Sealed ensures the class being inherited and
* object instantiation is restricted in the derived class
*/
public sealed class Singleton
{
private static int counter = 0;

/*
* Private property initilized with null
* ensures that only one instance of the object is created
* based on the null condition
*/
private static Singleton instance = null;

/*
* public property is used to return only one instance of the class
* leveraging on the private property
*/
public static Singleton GetInstance
{
get
{
if (instance == null)
instance = new Singleton();
return instance;
}
}
/*
* Private constructor ensures that object is not
* instantiated other than with in the class itself
*/
private Singleton()
{
counter++;
Console.WriteLine(“Counter Value ” + counter.ToString());
}
/*
* Public method which can be invoked through the singleton instance
*/
public void PrintDetails(string message)
{
Console.WriteLine(message);
}
}
}

Leave a Reply

75 Comments on "Singleton Design Pattern"

Notify of
avatar

Jaydeep Karena
Guest
Jaydeep Karena
2 months 12 days ago

really good video for learning Singleton design patterns

Avish MSD
Guest
Avish MSD
2 months 7 days ago

Thank you so much.

Robert Krewson
Guest
Robert Krewson
2 months 13 days ago

check out: U 2 B Z O N E . C O M – Get more attention that you deserve 🙂

ashutosh kumar thakur
Guest
ashutosh kumar thakur
2 months 13 days ago

Hi Avesh urgent request to you to upload the remaining episodes of Design patterns.Asked about the design pattern in many interviews.

Avish MSD
Guest
Avish MSD
2 months 7 days ago

Thank you so much. We are trying our best to post them as many as we can.

Ashish Agarwal
Guest
Ashish Agarwal
2 months 13 days ago

Hi, thanks alot for this series..I request you to upload the subsequent videos asap, you are doing a great job. Cheers! 🙂

Avish MSD
Guest
Avish MSD
2 months 7 days ago

Thank you so much. We are really honored.

Sovan Roy
Guest
Sovan Roy
2 months 13 days ago

In webfarm and webgarden how does the singleton instances are created and maintained? I mean in which core and how does the other core get the same instance?

Purusottam Ray
Guest
Purusottam Ray
2 months 13 days ago

Hello, thank you for your tutorials. It's really helpful .I was wondering how your auto suggestions come up when you type JS in Visual studio. Can you also please tell how to do that.Thanks

Purusottam Ray
Guest
Purusottam Ray
2 months 13 days ago

Ok…thank you

Shiva Prasad
Guest
Shiva Prasad
2 months 13 days ago

We have VS2015 and VS2017 (released latest). Based on the menu tool bars it looks like VS2015. Try with VS2015 as it comes as a free version with community edition.

Purusottam Ray
Guest
Purusottam Ray
2 months 13 days ago

It's 2016 i guess. I heard with newer version this functionality is not there. Is that so..?

Shiva Prasad
Guest
Shiva Prasad
2 months 13 days ago

Which version of visual studio are you using ? You should get it with VS2015

RAQIBUL ALAM RASHED
Guest
RAQIBUL ALAM RASHED
2 months 13 days ago

Thanks

deepak deshmukh
Guest
deepak deshmukh
2 months 13 days ago

hiAs u said in singleton only one instance is created and shared by all client object The same thing can be done using static class what is the difference between singleton and static class?

Ashish Agarwal
Guest
Ashish Agarwal
2 months 10 days ago
I hope u mean that this class cant b inherited rather than saying it cant inherit any other class.for ur reference u may try with this: public class Parent { public virtual void ParentMessage() { Console.WriteLine("from parent"); } } public sealed class SingleTon:Parent { private int count = 0; private SingleTon() { count++; Console.WriteLine("count is {0}", count); } public static SingleTon instance = null; public static SingleTon Instance { get { if (instance == null) return new SingleTon(); else return instance; } } public void Message(string msg) { Console.WriteLine(msg); } public override void ParentMessage() { Console.WriteLine("from singleton class"); } }class… Read more »
Sunny
Guest
Sunny
2 months 10 days ago

sure  mate , that can be done and I have seen singleton implementing Interfaces but as soon as Sealed is applied , this class becomes closed and not available for any inheritance 🙂

Ashish Agarwal
Guest
Ashish Agarwal
2 months 10 days ago

Hi Sunny,Thnks for ur insight ,though I meant inheriting another class from our singleton Class and not making our singleTon class to be inherited. This can be done,Isnt it?

Sunny
Guest
Sunny
2 months 11 days ago

He already marked class as sealed so thinking about inheritance is completely ruled out . with static it doesn't support multithreading . in order to solve multithreading there are 2 ways in this singleton pattern 1) lazy static initialization ( use readonly for the instance variable and the CLR will automatically take care of multithreading) 2) use double lock loading , create another instance object and lock that inside the constructor when checking for instance as null and creating an object . I am sure he will be doing this in Next video 🙂 Hope this helps – sunny

Ashish Agarwal
Guest
Ashish Agarwal
2 months 13 days ago

Hi Deepak,With Static classes we cant obtain other oops functionalities like inheritance.In case we want to inherit something from other classes , It would not be possible if we declare the class as static.Where as same can be achieved if we use Singleton pattern.Hope this helps.Ashish

Shiva Prasad
Guest
Shiva Prasad
2 months 13 days ago

Good point. May be he will explain later as this is the first version.

Transporter
Guest
Transporter
2 months 13 days ago

next time zoom in please ! couldn't see nothing !

Transporter
Guest
Transporter
2 months 7 days ago

thanks brother ! i'm sure the software ur using to record has feature to zoom in when editing ur video after recording

Avish MSD
Guest
Avish MSD
2 months 7 days ago

Sorry about that let me see what best i can do.

Marcin Grygierczyk
Guest
Marcin Grygierczyk
2 months 13 days ago

Awesome! Waiting for more 🙂 Thanks!

Avish MSD
Guest
Avish MSD
2 months 7 days ago

Thank you so much. We are trying our best to post them as many as we can.

wpDiscuz