Introduction
Welcome to the Design Pattern For Beginners article series. Here are a few parts; I will discuss various design patterns (in other words, a few of the most popular) targeting beginners. I will discuss most of the patterns in my own style and words. And before starting any pattern, we will try to find its basic need. So, let's begin our journey with a very nice quote.
- Design Pattern For Beginners - Part 1: Singleton Design Pattern
- Design Pattern For Beginners - Part 2: Factory Design Pattern
- Design Pattern For Beginners - Part 3: Prototype Design Pattern
- Design Pattern For Beginners - Part 4: Decorator Design Pattern
- Design Pattern For Beginners - Part 5: Composite Design Pattern
- Design Pattern For Beginners - Part 6: Adaptor Design Pattern
- Design Pattern For Beginners - Part 7: Bridge Design Pattern
- Design Pattern For Beginners - Part 8: memento Design Pattern
- Design Pattern for Beginners - Part-9: Strategy Design Pattern
- Design Pattern for Beginners - Part-10: Observer Design Pattern
- Design Pattern For Beginners - Part 11: Implement Decouple Classes in Application
"It's easy to walk on water and develop software from specifications when both are frozen."
Hey, that's not mine!! My poor memory says I read it somewhere in someone's blog. And I have forgotten where I read it. And I have forgotten where I read it. Please put a few keystrokes in the comments section if anyone has any information.
Anyway, let's return to the subject of this article. It's easy to develop software from specifications and when the requirements are constant. But by nature, people are not happy with the constant and fixed needs. (Yes, that's why language designers created variables! Ha... Ha...)
And here, design pattern come into play. If we implement a good design pattern we need not worry much when new requirements are added to the old ones. And here lies the importance of design patterns.
As I said earlier, this article series is targeted at young developers. If you fall in this category, then this introduction is enough to make explain "Why a design pattern is essential."
I am grateful to "Shivaprasad koirala, Sir"; from your video tutorial, I have heard the word "Design pattern" for the first time. Many thanks for providing the first of my design pattern journey.
Ok, that's a long introduction. Today let's start with a pervasive and easy design pattern called the "Singleton Design Pattern."
What is Singleton Design Pattern?
Let's learn why the Singleton Design Pattern is implemented. And then, we will see how to implement it in C#.
Basic need: Think there is a hit counter in any web application. (I know you have already seen many examples like this). Now, when a visitor hits a web application, it will increase by one. This is one scenario where we can implement the Singleton Design Pattern.
Or think about another situation where you want to share a single instance of a class across various threads.
We can implement the singleton pattern in one of two ways.
1. Make the constructor private so that o one can create an object of the singleton class
Have a look at the following code to understand the basic concept.
using System;
using System.Collections;
using System.Data.SqlClient;
namespace Test1
{
class singleTon
{
public static int counter = 0;
private singleTon()
{
//Private constructor will not allow create instance
}
public static int Returncount()
{
return counter;
}
public static void IncreaseCount()
{
counter++;
}
}
class Program
{
static void Main(string[] args)
{
//singletone design pattern
Console.WriteLine("Before increase : " + singleTon.counter);
singleTon.IncreaseCount();
Console.WriteLine("After Increase:" + singleTon.counter);
Console.ReadLine();
}
}
} 


Om PrakashPosted Jan 28, 2023, 6:56 AM
Great Article. Thanks
Brett WertzPosted Jun 11, 2020, 4:07 AM
Sourav, thanks for this article! You said "If it is null then no instance is created of this class and if it is not null then an instance has already been created and it's time to return the previously created instance." Shouldn't the wording be, "If it is null, a singleTon instance *is created* and if it is not null, then the previously created instance is returned."
lakshmana poojaryPosted Sep 16, 2014, 5:54 AM
Nice one.. thank you.
Alex CaoPosted Sep 8, 2013, 1:31 AM
I'am learning DP now,your artical is very helpful.Thank you.
sparkle daiPosted Aug 27, 2013, 8:25 PM
So good!I often get instance by a method.But,you get instance by a property.Let me know singleton parttern again.
Sandeep Singh ShekhawatPosted Aug 3, 2013, 12:03 AM
good post as expected.Your article quote is taking from Book "System Analysis and Design " whose author is Edward V Berard. I am reading this book in these days.
Sourav KayalPosted Aug 2, 2013, 11:33 AM
Thanks sam, for your comment. I am expecting your valuable suggestion(s) to improve quality of my article. Thanks again.
Sam HobbsPosted Aug 2, 2013, 10:43 AM
This article is good. I appreciate that you are attempting to explain a complex and important topic in a new way. There is something about performance (as in your previous articles) that I feel strongly about but I have been unable to explain.