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

Nihal Dev
Guest
Nihal Dev
16 days 9 hours ago

Great.

Md Abuzar
Guest
Md Abuzar
24 days 1 hour ago

Sir, Could you please tell me what is the real world example in which we must need to create Singleton class or use singleton pattern.

saman khalifa
Guest
saman khalifa
1 month 4 days ago

No matter how intelligent you may sound, I can't take you seriously.

Mohammed Hassan
Guest
Mohammed Hassan
1 month 6 days ago

Great Job, waiting for SharePoint 2016 Development 🙂

Arvind Kumar Singh
Guest
Arvind Kumar Singh
1 month 22 days ago

Thanks Avish for your Good Explanation.I want to know what is the main difference between Singleton class and static class.

Avish MSD
Guest
Avish MSD
1 month 15 days ago

Thank you very much Arvin kumar. We will be covering your points in the next session.

Blue Sky
Guest
Blue Sky
1 month 24 days ago

Nicely Explained.. :). You cleared the concept in bits and pieces. Beautifully explained.

Avish MSD
Guest
Avish MSD
1 month 15 days ago

Thank you so much Blue Sky. Really honored with you feedback.

NIkola Vitorovic
Guest
NIkola Vitorovic
1 month 24 days ago

We appreciate your work a lot,keep doing it :D! And for the others who learn from this channel,tell your friends about it,if we cant support them with money,we can at least subscribe this channel and watch ads! Cheers from Serbia!

Avish MSD
Guest
Avish MSD
1 month 15 days ago

Thank you so much Nikola Vitorovic.

Mayank Topiwala
Guest
Mayank Topiwala
1 month 27 days ago

Why the screen is flickering during the demo?

Avish MSD
Guest
Avish MSD
1 month 15 days ago

Sorry about that Mayank. Will be corrected in the next sessions

Vaddiparthi Venkata Murali Krishna
Guest
Vaddiparthi Venkata Murali Krishna
1 month 28 days ago

it's very good and useful. could you please upload few more videos

MhRiderGaurav
Guest
MhRiderGaurav
2 months 6 days ago

this is not real kuduvenkat .

Avish MSD
Guest
Avish MSD
1 month 15 days ago

Sorry to hear you are not Happy. Venkat is busy with MVC and lot of other stuff. We both are buddies and will try to help as best as we can.

1 2 3 4
wpDiscuz