• Post Reply Bookmark Topic Watch Topic
  • New Topic

Singleton??  RSS feed

 
Rafa Barcel� Bauz�
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello.
I have been listening about singleton, i have tried to find information on simple examples, but i still can understant what a singleton class is for?

Can someone put an example as simple as possible and explain why Singleton classes are needed in the real world???

Thanks!!!
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This sort of question gets asked a lot. Have a search through this forum, you'll find loads of examples & discussions about Singletons.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A singleton is a class of which no more than one instance is created, as you know.

An example: Suppose you have a class Keyboard, that represents the keyboard connected to the computer. There is only one keyboard connected to the computer, so you wouldn't want more than one instance of class Keyboard in your program.

Note that some people regard the Singleton design pattern as an "anti-pattern", i.e. bad practice. See here for discussions:

Singleton Considered Stupid
Why Singletons are Evil
Use your singletons wisely
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[pedant mode]Actually you can have a singleton with two instances. The singleton pattern is used to create a predetermined and fixed number of object,s usually one.[/pedant mode]
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16057
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Shouldn't those be called Doubleton, Tripleton, Quadrupleton etc...?
 
Naseem Khan
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper Young:
Shouldn't those be called Doubleton, Tripleton, Quadrupleton etc...?


Yes I agree. Its multiton design pattern. In multiton, we basically maintain instances by a [key, value] pair in a Map. So instead of having a single instance per application, multiton pattern ensures single instance per key. Altough this design pattern is not mentioned anywhere in official Gang of Four Design pattern.

Naseem
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The GoF description does not specify a single instance, but a controlled number of instances and a mechanism to get them. A single instance per key just sounds like a Singleton holding a map. I like this line from WikiPedia:

It is also considered an anti-pattern since it is often used as a euphemism for global variable.


The emphasis on "euphemism" was mine ... it seems to convey just the right amount of disdain.

In spite of the potential for misuse, I use Singleton for caches and pools now & then.
[ September 05, 2006: Message edited by: Stan James ]
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Stan James:
The GoF description does not specify a single instance, but a controlled number of instances and a mechanism to get them. A single instance per key just sounds like a Singleton holding a map. I like this line from WikiPedia:



The emphasis on "euphemism" was mine ... it seems to convey just the right amount of disdain.

In spite of the potential for misuse, I use Singleton for caches and pools now & then.

[ September 05, 2006: Message edited by: Stan James ]


The GoF book I have specifically (and repeatedly) refers to it as one, sole instance. Of course, even that being the case and assuming my copy is a precise printing I wouldn't say it's true just because "the GoF says so". Personally, I prefer referring to anything that has a controlled number of instances that may be more than one as a Multiton, but that's only my opinion.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Campbell Ritchie:
[pedant mode]Actually you can have a singleton with two instances. The singleton pattern is used to create a predetermined and fixed number of object,s usually one.[/pedant mode]


You can but then you'd have a contradiction (singleton, two instances, enough said?) so you can't - a fundamental method of reasoning.

A better explanation is that a singleton cannot exist, but we've been down that road without success before, so I suggest we let it be in the context of this forum.

Another conclusion is that "since a critical mass of software developers rarely use or at least acknowledge any more than the notion of 1 class loader, we as a community are prepared to conclude that class loader scoped data is referred to as a singleton".

This can further be supported by "since a critical mass of software developers often use or at least acknowledge more than the notion of 1 object, we as a community are not prepared to conclude that object-scoped data (e.g. instance fields) is referred to as a singleton".

This is (obviously) clearly a flawed method of reasoning (there is much literature on it actually).

Edit: grammar

I apologise if I stir up the nest, but I am compelled to purport sensible logic.
[ September 05, 2006: Message edited by: Tony Morris ]
 
Guy Allard
Ranch Hand
Posts: 776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ken Blair:


The GoF book I have specifically (and repeatedly) refers to it as one, sole instance. ....


The one I have, on page 128, under the heading "Consequences", lists as point 4:

Permits a variable number of instances.

Guy
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Guy Allard:


The one I have, on page 128, under the heading "Consequences", lists as point 4:

Permits a variable number of instances.

Guy


Which refers to the ease of changing your mind at a later date and allowing more than one instance. Arguably that would no longer be a "Singleton" but something similar that uses the same approach.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Tony]: I apologise if I stir up the nest, but I am compelled to purport sensible logic.

That's a telling use of "purport" there, Tony.

[Ken]: Arguably that would no longer be a "Singleton" but something similar that uses the same approach.

GoF does say repeatedly that they're talking about one instance, period, and then they throw that one sentence in and muck everything up. The sentence does seem to imply that the multiple instances are still singletons, thus contradicting their own text elsewhere. But really, ignoring GoF, if we want to talk about multiple instances, "Singleton" is a pretty crappy choice of word to use anyway. So I'd just accept that "singleton" means one instance (or zero) to many people, but it may mean multiple instances to some people. It's easier to just ask the other party which one they mean, when it comes up, than it is to try to standardize the meaning now after GoF made a hash of it. Most patterns have multiple variations anyway; there's little point in getting locked into one particular interpretation as if it's canonical.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it really matter?

Patterns are not meant to be rigid. They can(and should be) adapted to specific situations.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Agreed; that's what I was getting at in my last sentence.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The GoF book I have specifically (and repeatedly) refers to it as one, sole instance.


Guess I'll have to dig the book back out. Standing corrected ...
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jim Yingst:
Agreed; that's what I was getting at in my last sentence.


Sorry, I didn't get to your post. Just saw too many posts implying that patterns were concrete, and shot off a knee-jerk reply.
 
Dana Bothner-By
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This discussion reminds me of the final comment Woody Allen wrote for a fictitious Philosophy course:

Philosophy : Everyone from Plato to Camus is read and the following topics are covered :
  • Ethics : The categorical imperative, and six ways to make it work for you.
  • Aesthetics : Is art the mirror of life, or what?
  • Metaphysics : What happens to the soul after death? How does it manage?
  • Epistemology : Is knowledge knowable? If not, how do we know this?
  • The absurd : Why existence is often considered silly, particularly for men who wear brown-and-white shoes.


  • Manyness and oneness are studied as they relate to otherness.
    (Students achieving oneness move ahead to twoness.)
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!