Topics
Published on May 13, 2017 by kudvenkat
Want create site? Find Free WordPress Themes and plugins.

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);
}
}
}

Did you find apk for android? You can find new Free Android Games and apps.

Leave a Reply

82 Comments on "Singleton Design Pattern"

Notify of
avatar

Tofael Ahmed
Guest
Tofael Ahmed
6 months 3 days ago

GRATE, waiting for next one.

Avish MSD
Guest
Avish MSD
6 months 1 day ago

Thank you. We are trying our best to upload as many as we can.

waqar abbas
Guest
waqar abbas
6 months 3 days ago

i expected this series from kudvankat sir,..but you are also very good.Do your best as kudvankat sir done .because many people even every one is comparing your standard with kudvankat.i am sorry you are not good as kudvankat that i can understand.and choice examples like kudvankat do because his examples talk every thing like standard, concept, etc..keep it up …. Thank You…

Avish MSD
Guest
Avish MSD
6 months 1 day ago

Thank you Mr Waqar Abbas. Really honored with your inputs. Yes, Vekat is absolutely fantastic totally agree with you. We are trying our best to come up with many examples for each and every line and declaration of a patternso that its very easy for any one to understand the design pattern. We will try to do our best. Thanks again.

Dattatray Pidurkar -pitrya
Guest
Dattatray Pidurkar -pitrya
6 months 3 days ago

I liked this new series of tutorial. I do have a question though. How frequently you are going to put tutorial videos?

Avish MSD
Guest
Avish MSD
6 months 1 day ago

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

Andres Gonzalez
Guest
Andres Gonzalez
6 months 4 days ago

Too private static Program Instance = null;public static Program ProgramSingleton => Instance ?? new Program();

Avish MSD
Guest
Avish MSD
6 months 1 day ago

Thank you Mr Andres Gonzalez. We will discuss this in one of the upcoming sessions.

Zbigniew Ćwiąkalski
Guest
Zbigniew Ćwiąkalski
6 months 4 days ago
Hi ! I have a question. Why not utilize static class instead of Singleton pattern ? Is it equal to Singleton pattern ? What are potential downsides of this solution ? public static class Singleton { private static int intInstanceCounter = 0; public static void Message(String message) { Console.WriteLine(message); } static Singleton() { intInstanceCounter++; Console.WriteLine("Singleton instance counter = " + intInstanceCounter); } }
waqar abbas
Guest
waqar abbas
6 months 3 days ago

yes i have a same Question..why he did not use static class…?

Ashish Agarwal
Guest
Ashish Agarwal
6 months 3 days ago

Hi Pls see the conversation on comment of "deepak deshmukh"'

ruthy Ayele
Guest
ruthy Ayele
6 months 5 days ago

thanks Avish! it is great video

Avish MSD
Guest
Avish MSD
6 months 1 day ago

Thank you so much Mr Ruthy Ayele.

Yo Yo Funny Singh
Guest
Yo Yo Funny Singh
6 months 6 days ago

sir please add video for factory pattern

Avish MSD
Guest
Avish MSD
6 months 1 day ago

Sure Mr Yo Yo Funny Singh. We will be covering all the patterns with real world examples.

Jaydeep Karena
Guest
Jaydeep Karena
6 months 6 days ago

really good video for learning Singleton design patterns

Avish MSD
Guest
Avish MSD
6 months 1 day ago

Thank you so much.

Robert Krewson
Guest
Robert Krewson
6 months 6 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
6 months 7 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
6 months 1 day ago

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

wpDiscuz