Singleton design pattern

Singleton design pattern

HomekudvenkatSingleton design pattern
Singleton design pattern
ChannelPublish DateThumbnail & View CountDownload Video
Channel AvatarPublish Date not found Thumbnail
0 Views
1. What is the Singleton design pattern?
2. Singleton as a generation pattern
3. Guidelines for implementation
4. How do we implement a singleton class

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

Eating healthy is very important for both the body and mind. If you like Aarvi Kitchen recipes, please support us by sharing, subscribing and liking our YouTube channel. I hope you can help.
https://www.youtube.com/channel/UC7sEwIXM_YfAMyonQCrGfWA/?sub_confirmation1

Guidance systems
http://csharp-video-tutorials.blogspot.com/2017/05/singleton-design-pattern_13.html

Design Patterns Tutorial
https://www.youtube.com/playlist?listPL6n9fhu94yhUbctIoxoVTrklN3LMwTCmd

All Dot Net and SQL Server tutorials in English
https://www.youtube.com/user/kudvenkat/playlists?view1&sortdd

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

This is the continuation of Part 1 of the Design Patterns tutorial, so please watch Part 1 before continuing.

The Singleton pattern belongs to the Creation type. As discussed in our previous video, Gang of Four has defined five design patterns that belong to the category of creation types. Singleton is one of them and the rest are Factory, Abstract Factory, Builder and Prototype patterns. As the name suggests, the creation type deals with object creation mechanisms. To simplify this, the creation pattern basically tells us about creating objects in a way that is suitable for a particular situation.

The Singleton design pattern is used when it is necessary to ensure that only one object of a particular class is instantiated. This single created instance is responsible for coordinating actions throughout the application.

Benefits and guidelines for singleton implementation.

Concurrent access to the resource is well managed by the 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 as private. To control singleton access, we also need to provide a static property that returns a single instance of the object.

Implementation example for singleton class

Program.cs

use system;
with System.Collections.Generic;
with System.Linq;
with System.Text;
using System.Threading.Tasks;
// First version of the Singleton demo
Namespace SingletonDemo
{
Class program
{
static void Main(string[] args)
{
/*
* Assuming Singleton is created from Employee class
* we refer to the GetInstance property of the Singleton class
*/
Singleton from Employee Singleton.GetInstance;
fromEmployee.PrintDetails(/"From Employee/");
/*
* Assuming Singleton is created from Student class
* we refer to the GetInstance property of the Singleton class
*/
Singleton from Student Singleton.GetInstance;
fromStudent.PrintDetails(/"From Student/");

Console.ReadLine();
}
}
}

Singleton.cs
use system;
with System.Collections.Generic;
with System.Linq;
with System.Text;
using System.Threading.Tasks;
Namespace SingletonDemo
{
/*
* Sealed ensures that the class is inherited and
* Object instantiation is restricted in the derived class
*/
public sealed class Singleton
{
private static int counter 0;

/*
* Private property initialized to zero
* ensures that only one instance of the object is created
* based on the zero condition
*/
private static singleton instance null;

/*
* public property is used to return only one instance of the class
* Exploitation of private property
*/
public static singleton GetInstance
{
receive
{
if (instance null)
Instance new Singleton();
return instance;
}
}
/*
* Private constructor ensures that the object is not
* instantiated other than within the class itself
*/
private Singleton()
{
Switch;
Console.WriteLine(/"Counter value /" counter.ToString());
}
/*
* Public method that can be called from the singleton instance
*/
public void PrintDetails(string message)
{
Console.WriteLine(message);
}
}
}

Please take the opportunity to connect with your friends and family and share this video with them if you find it useful.