A Developer Journey who codes for fun

Daily Dose Of Code

  • Home
  • Dot.Net Basics
    • .Net Basics
      • CTS
      • CLS
      • CLR
      • Strong Vs Weak Ref
      • .Net Framework
      • What is Manifest
    • Memory Management
      • Garbage Collection 1
      • Garbage Collection 2
      • Circular Reference
  • C Sharp
    • Abstract Class in C#
    • Interfaces in C#
    • Value type by Val and By Ref
    • Var keyword
    • Null Coalescing Operator
    • Buit-in code snippets
  • OOPS
    • Abstraction and Encapsulation
    • Polymorphism
    • Inheritence
    • Aggregation
  • Threading
    • Delegates
      • Calling Delegate using Invoke, BeginInvoke
      • Multicast Delegate
      • Exception Handling in Multicast Delegate
      • Action
      • Predicate
      • Func
    • Synchronization
    • Thread Pool
    • Exception Handling
    • TPL
  • Design Pattern
    • Creational Patterns
      • Singleton Pattern
      • Factory Pattern
      • Abstract Factory Pattern
      • Prototype Pattern
      • Builder Pattern
    • Structural Patterns
      • Adapter Pattern
      • Bridge Pattern
      • Composite Pattern
      • Proxy Pattern
      • Facade Pattern
      • Decorator Pattern
      • Flyweight Pattern
    • Behavioral Patterns
      • Command Pattern
      • Interpreter Pattern
      • Iterator Pattern
      • Mediator Pattern
      • Memento Pattern
      • Observer Pattern
      • State Pattern
      • Strategy Pattern
      • Visitor Pattern
      • Chain Of Responsibility Pattern
      • Template Pattern
  • Data Structures
    • Generic List in C#
    • 2d array to 1d array
    • 3d arrayto 1d array
    • Linked List
      • Singly Linked List in C#
    • Queue
      • Dummy Data 1
    • Stack
      • Dummy Data 2
    • Tree
      • Dummy Data 3
    • Graph
      • Dummy Data 4
  • WCF
    • WCF Service using VS 2015
  • Scripts
    • Chrome Extensions
      • Create a Chrome Extension
      • Facebook autologout script
      • Gmail autologout script

Singleton Pattern

 Unknown     10:08 PM     4 comments   

Singleton pattern is a design pattern that is used to restrict instantiation of a class to one object. This is very useful when only there is a need of single object which handles all the actions across the system.This pattern restrict the instantiation to a certain number of objects and this concept is to generalize the systems to operate more efficiently when only one object exists.
Example :-
The Abstract Factory, Builder, and Prototype patterns can use Singletons in their implementation

Implementation of Singleton pattern :-
///
/// Thread-safe singleton example without using locks
///

public sealed class SingletonClass
{
private static readonly SingletonClass singletonInstance = new SingletonClass();

// Explicit static constructor to tell C# compiler
// not to mark type as beforefieldinit
static SingletonClass()
{
}

private SingletonClass()
{
}

///
/// The public Instance property to use
///

public static SingletonClass SingletonInstance
{
get { return singletonInstance ; }
}
}
Happy to code...
Hiii...One more example to clear singleton approach :-
I have implement progress bar for ma current application so i am giving some reference here:-
#region Private Fields

private static StatusProgressBar _instance = null;

#endregion

#region Constructor

private StatusProgressBar()
{
this.Style = ProgressBarStyle.Blocks;
this.Step = 1;
}

#endregion

#region Properties

///
/// Get Singleton instance of progressBar
///

public static StatusProgressBar Instance
{
get
{
if (_instance == null)
{
_instance = new StatusProgressBar();
}

return _instance;
}
}

#endregion
Made the above class as public and access it suppose class name is StatusProgressBar

StatusProgressBar.Instance.PerformStep();
then if instance is already running then it will return that instance otherwise will return a new instance.
  • Share This:  
  •  Facebook
  •  Twitter
  •  Google+
  •  Stumble
  •  Digg
Email ThisBlogThis!Share to XShare to Facebook

Related Posts:

  • SQL - Tools (WHERE, AND, OR Clause)Where clause is use for filtering like :-SELECT * FROM table_name WHERE column_name (operator) value(operator) :- =, , ,! and more operators.A Friends… Read More
  • SQL - Tools....(SELECT Clause)SELECT :-SELECT * FROM TABLE_NAME this will select whole table If Coils table contains 5 columns p_id, coil_no, coil_width, coil_length, coil_name.Th… Read More
  • SQL - Tools(ORDER BY)ORDER BY keyword is used for sorting suppose in a table there is a column named first_name.If you want to show the result in ascending order then :-S… Read More
  • SQL - Tools ( INSERT,UPDATE and DELETE Statement)INSERT INTO table_nameVALUES (value1, value2, value3,...)IF a table Persons contains P_Id,LastName,FirstName,Address,City columns.If you want to inser… Read More
  • Visual Studio .Net ShortCut keysDecreaseFilterLevel : ALT + ,IncreaseFilterLevel : ALT + .GotoBrace : CTRL + ]GotoBraceExtend : CTRL _ SHIFT + ]LineEnd … Read More
Newer Post Older Post Home

4 comments:

  1. abhinavSeptember 28, 2009 at 11:15 PM

    Very good post thanks to post this....I m a regular visitor of your blog keep posting....

    ReplyDelete
    Replies
      Reply
  2. AnonymousOctober 1, 2009 at 5:26 AM

    Saurabh I have a question in my mind;
    1.Can you please Explain the Code and explain how is it restricting the cloning of the object. As per my knowledge If object of this class Can be cloned then it will not be Singleton class .
    2. Why do we need a Singleton class ? tell me any situation where use of singleton class is indispensible.

    Thanks in Advance

    Rajesh Kumar

    ReplyDelete
    Replies
      Reply
  3. UnknownOctober 6, 2009 at 1:51 AM

    Hi rajesh...
    1 :- In real world if you are asking where you need singleton class then i have a instance in ma mind i had a class in which there was 10 + event calling was handled and first time when a object made then it call and handle all those events but again when i need to use a method of this class in some other solution then i need to create object(i couldn't make static) there but if there is already a object running then there was no need to create a new instance that can give me performance hit due to 10+ event calling so i use theat instance using singleton pattern .Thats the real example which i faced.

    2:- In the above code when you already create a object first time then if there is need to access some method,property in some other class and object is already running then call the property which gives you instance that will restrict the cloning of object.

    ReplyDelete
    Replies
      Reply
  4. UnknownOctober 6, 2009 at 1:54 AM

    Saurabh singh :-
    This will make your problem more clear
    Ensure a class has only one instance, and provide a global point of access to it.

    ReplyDelete
    Replies
      Reply
Add comment
Load more...

About The Author

Unknown
View my complete profile

Total Pageviews

84537

Popular Posts

  • Clr - Common Language Runtime
    .Net framework provides a run time environment - CLR. Common language runtime takes the IL code from the compiler( language specific) and p...
  • Auto logout chrome extension for Gmail
    Hello Friends, In the last article we learned to create a sample chrome extension. Here we are going to create auto logout Gmail script as...
  • Predicate delegate in C#
    Hello Everyone, In the article we will talk about Predicate delegate. Predicate is also a delegate which encapsulate a method that takes...
  • .Net Framework overview
    Hello friends : Here i am writing my first article on .Net framework anyways....So the question is What is .Net Framework ? The .Net fram...
  • Nagarro Placement Papers..
    Ques.1 :- Seat Reservation prog for the theatre. Write a function for seat allocation for the movie tickets. Total no of seats available are...
  • What does it mean by disconnected data access architecture of ADO.Net?
    ADO.Net introduces the concept of disconnected data architecture. In traditional data access components, you make a connection to the databa...
  • Calling the Delegates using Invoke(), BeginInvoke() and DynamicInvoke() ?
    Hello Guys, So in the last article we talked about What is delegate and how can we create a delegate. In this article we will discuss w...
  • C code to Check the string has valid identifier or not in.
    #include #include #include char keyword[][10]={"auto","break","case","char","const","...
  • Garbage Collection - Automatic Memory Management Part II
    Welcome friends in the second article of Garbage Collection. Those who have missed the first one can visit here . So in this article i will...
  • Delegates in C Sharp
    A Delegate is a type variable that holds the reference to a method. Delegates are similar to Pointer to functions in C and C++ When we...

Blog Archive

  • ►  2016 (4)
    • ►  September (2)
      • ►  Sep 03 (2)
    • ►  August (1)
      • ►  Aug 28 (1)
    • ►  April (1)
      • ►  Apr 24 (1)
  • ►  2015 (12)
    • ►  September (10)
      • ►  Sep 30 (1)
      • ►  Sep 29 (1)
      • ►  Sep 28 (1)
      • ►  Sep 27 (2)
      • ►  Sep 26 (3)
      • ►  Sep 20 (1)
      • ►  Sep 19 (1)
    • ►  August (1)
      • ►  Aug 16 (1)
    • ►  March (1)
      • ►  Mar 31 (1)
  • ►  2013 (10)
    • ►  June (1)
      • ►  Jun 16 (1)
    • ►  April (1)
      • ►  Apr 21 (1)
    • ►  February (8)
      • ►  Feb 18 (3)
      • ►  Feb 17 (2)
      • ►  Feb 16 (2)
      • ►  Feb 15 (1)
  • ►  2012 (1)
    • ►  May (1)
      • ►  May 27 (1)
  • ►  2010 (22)
    • ►  October (14)
      • ►  Oct 21 (1)
      • ►  Oct 06 (12)
      • ►  Oct 04 (1)
    • ►  April (2)
      • ►  Apr 22 (1)
      • ►  Apr 16 (1)
    • ►  March (1)
      • ►  Mar 30 (1)
    • ►  January (5)
      • ►  Jan 08 (3)
      • ►  Jan 01 (2)
  • ▼  2009 (110)
    • ►  December (8)
      • ►  Dec 18 (2)
      • ►  Dec 05 (1)
      • ►  Dec 04 (5)
    • ►  November (1)
      • ►  Nov 27 (1)
    • ►  October (14)
      • ►  Oct 09 (4)
      • ►  Oct 07 (1)
      • ►  Oct 06 (3)
      • ►  Oct 05 (3)
      • ►  Oct 01 (3)
    • ▼  September (17)
      • ►  Sep 30 (1)
      • ►  Sep 29 (1)
      • ▼  Sep 28 (1)
        • Singleton Pattern
      • ►  Sep 25 (1)
      • ►  Sep 24 (1)
      • ►  Sep 17 (2)
      • ►  Sep 15 (3)
      • ►  Sep 11 (2)
      • ►  Sep 09 (3)
      • ►  Sep 08 (2)
    • ►  August (31)
      • ►  Aug 31 (1)
      • ►  Aug 27 (3)
      • ►  Aug 26 (1)
      • ►  Aug 25 (2)
      • ►  Aug 24 (1)
      • ►  Aug 22 (2)
      • ►  Aug 21 (3)
      • ►  Aug 20 (2)
      • ►  Aug 19 (3)
      • ►  Aug 18 (1)
      • ►  Aug 16 (1)
      • ►  Aug 12 (2)
      • ►  Aug 11 (1)
      • ►  Aug 10 (3)
      • ►  Aug 07 (4)
      • ►  Aug 06 (1)
    • ►  July (24)
      • ►  Jul 25 (4)
      • ►  Jul 24 (20)
    • ►  April (15)
      • ►  Apr 10 (3)
      • ►  Apr 07 (9)
      • ►  Apr 06 (3)

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments
copyright @ TechGiant 2015. Powered by Blogger.

Disclaimer

This is my personal blog and i write articles on .Net, WPF, C#, OOPS, Threading and other .Net technologies. This is not related to any of my employer and organizations. This is the result of my personal interest.

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Followers

Copyright © 2025 A Developer Journey who codes for fun | Powered by Blogger
Design by Hardeep Asrani | Blogger Theme by NewBloggerThemes.com