Singleton Design Pattern

preview_player
Показать описание
1. What is Singleton Design Pattern
2. Singleton as Creational Pattern
3. Implementation Guidelines
4. How do we implement a Singleton class

Text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

Slides

Design Patterns Tutorial

All Dot Net and SQL Server Tutorials in English

All Dot Net and SQL Server Tutorials in Arabic

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

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

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);
}
}
}
Рекомендации по теме
Комментарии
Автор

This was so clear and up to the point. Exactly what I was looking for! Thank you so much!

anuragparcha
Автор

Finally design patterns started waiting for this series since

parthmashroo
Автор

"Let's flip to Visual Studio" ... reminds me of ol' venkat

carlandres
Автор

Even with your THICC accent, your explainations are more comprehensive than most other peoples' attempts.

Mortuus
Автор

Beautifully explained. Now got clear understanding of Singleton pattern. Thanks.

vaishalibhalerao
Автор

I was scared of design patterns as I couldn't understand anything. Now I am super clear about what Design Patterns are. Thank you so much ❤️

dssagar
Автор

I love it when you say "Look at that" lol. Great lesson.

keithkatane
Автор

I have been waiting for long time for design pattern videos, Thank you sooo much for starting this series

farooqmd
Автор

Thank you so much Avish. You're a great teacher. You make me better!

kamdemkakengne
Автор

Thanks for the explanation. However, I think you could have taken a better example because the problem you described can be addressed by making PrintDetails method static in Singleton class you defined.

AishwaryaChamanoor
Автор

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

kunaldedhia
Автор

Very clean and precise explanantion bro. just upto the mark.

Nicetrycutiepie
Автор

I did not see that before.. Why you typed: public static name of the class(Singleton) instance = 0, same you did with that method named GetInstance. What is the use of putting the name of the class in there? Can you give me some documentation about that and explain me plase! And what is public static Singleton instance = null; it is a variabile ? what is that? i dint se no int, double, float.. etc

bogdanmitroii
Автор

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

ashishagarwal
Автор

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.

mdabuzar
Автор

Thank you. This is good but its very basic. You need to show how to handle singleton when multiple clients are instantiating the class by implementing threads or with more newer version of Lazy loading.

sri
Автор

What if there are multiple requests to a website on multiple thread to a single action method in that case will it be only one instance available across all the threads ?

CanadaKeBhai
Автор

I saw lots of videos but this is superb..

zuzuba
Автор

Will we use design pattern all across the pages or only in one page in .Net
?

S-yfte
Автор

Suppose we have a Printer class which takes the document from local hard disk and prints into hard copy. One way of initialise without singeton would hang the system as multiple request are coming to this class but with singelton makes system robust and performant.

abhiraj