Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

what is different in singleton design pattern

 
ajay verma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as i know,we can't create a object outside a class because we are using a private constructor inside a class.
can any one suggest me the exact definition of singleton with the the help of programming?
kindly suggest me with the exact definition with programming!


thanks
 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The 'single'ton uses a single instance instead of two or more, for performance-related reasons.

There is a static method in the singleton class that any outside class can call. Inside that method the class instantiates an instance of its class, and returns it to the outside caller. That is how it is instantiated.

Why singleton? If you have one million transactions a day which can possibly create/instantiate one or more of a certain class, that can put a performance hit on you server. If a single instance can serve all those multi-million transactions, then you improve the resource statistics of your server by a big factor. You only instantiate one, instead of say 5 million! (Memory/resources speaking: what a relief!)
 
ajay verma
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi jesus

as per my request kindly suggest me with the help of programming , in which the particular instance is serving to million of transaction.



thanks again
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ajay verma:
can any one suggest me the exact definition of singleton with the the help of programming?


Yes, see:
http://en.wikipedia.org/wiki/Singleton_pattern
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oo Patterns Uml Refactoring FAQ: Singleton Pattern
[ February 03, 2008: Message edited by: Peer Reynders ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that in addition of only having one instance of a class, the Singleton pattern also provides a global access point (a static getter). This can lead to a nasty coupling problem. Fortunately there are alternative patterns that too only provide one instance, for example the Just Create One pattern.
 
chandrakant karale
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as per my request kindly suggest me with the help of programming , in which the particular instance is serving to million of transaction.


Hi Ajay,
Definition and example .

The Singleton Pattern is a class of which there can be no more than one instance. It provides a single global point of access to that instance.


 
Jesus Angeles
Ranch Hand
Posts: 2068
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by chandrakant karale:


Hi Ajay,
Definition and example .

The Singleton Pattern is a class of which there can be no more than one instance. It provides a single global point of access to that instance.




You will find a good introduction, explanation, code, and issues about the code implementation, in Head First Design Patterns.

The code above has issues and needs improvement. But yes, above is a great example on how to get an idea of what a singleton is.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic