aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes simplest singleton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "simplest singleton" Watch "simplest singleton" New topic
Author

simplest singleton

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830


Qunfeng Wang
Ranch Hand

Joined: Jan 28, 2005
Posts: 433
Does this invalid Encapsulation? I mean it's not wise to keep the memeber varible public to get the simplest sigleton.


To be or not to be. It's a question.
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
The only problem with it is that the variable is not final, so you could reassign it to be null.

There's no invalidation of encapsulation, as there's nothing that should be hidden.

Of course, you don't need to do anything clever to have a singleton; just instantiate a class only once, and it's a singleton.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Ricky Clarkson:

Of course, you don't need to do anything clever to have a singleton; just instantiate a class only once, and it's a singleton.


That would be a Just Create One, my favorite pattern over Singleton.

The Singleton pattern adds a global access point to get the instance.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Heh, with a final I like the OP's suggestion fine. I surely give it higher marks than this one from
James W Cooper at IBM TJ Watson Research

He has a book chapter length exposition on this that never gets around to a static method to access the instance. That gives it less of a "global" feel, but ...


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
That's really an Anti-Pattern, isn't it?
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
Originally posted by Ricky Clarkson:
Of course, you don't need to do anything clever to have a singleton; just instantiate a class only once, and it's a singleton.


Originally posted by Ilja Preuss:
That would be a Just Create One, my favorite pattern over Singleton.

The Singleton pattern adds a global access point to get the instance.


I'd say that's a global variable (regardless of whether it can vary), and not really related to Singleton at all. A singleton can exist without a global access point, and a global access point can exist for a class that is not a singleton. For example, I might instantiate a class exactly twice, providing two global access points. I don't have two singletons.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
The GOF one-liner for Singleton: Ensure a class has only one instance, and provide a global point of access to it. Under applicability they repeat ... it must be accessible to clients from a well-known access point. A single instance without the global may be the perfect solution but it only scores 50% in GOF.

How much do we care about that? I'm the one who brings up all kinds of factory variations (with lower case "f") that aren't in GOF, so to be consistent I should say "not much as long as it works."
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Ricky Clarkson:
I'd say that's a global variable (regardless of whether it can vary), and not really related to Singleton at all.


Well, a Singleton *is* an elaborate global variable. Seriously.

A singleton can exist without a global access point


Not by the GoF definition. And to answer Stan's question "why should we care?": Because one of the big advantages of patterns is that they give us a common vocabulary. But that only works well when we agree on the definition.

a global access point can exist for a class that is not a singleton. For example, I might instantiate a class exactly twice, providing two global access points. I don't have two singletons.


Sure. The global access point is required, but not sufficient, for a class to be called a Singleton.
Ricky Clarkson
Ranch Hand

Joined: Jul 27, 2006
Posts: 131
I haven't seen the relevant passage from GoF. I based my assertions on the wikipedia article. I've merely dipped into GoF in bookshops. It would cost me a whole hour's wages..

Hence my assertion that a singleton is not an elaborate global variable, but merely a class that is instantiated exactly once.


And to answer Stan's question "why should we care?": Because one of the big advantages of patterns is that they give us a common vocabulary. But that only works well when we agree on the definition.


Language often helps us to think, and obviously helps us to express our thoughts, but it often constrains us, too. I read this book by George Orwell - http://www.lifehack.org/articles/lifehack/5-rules-of-effective-writing-by-george-orwell.html (note that the website itself cannot count). Take a look at the points outlined from the link. That is, at the end of the day, when all's said and done, there's nothing left to say and do. In technical terms, when you have a singleton, a factory and a visitor, you don't really have anything, just a framework for looking like you know what you're talking about. You can probably write for theregister that way.

It's not unlike the experience of hearing a child utter profanities - I remember asking my mum repeatedly what '**** ' meant, while we were out shopping. She wouldn't tell me, but kept hitting me around the head (not hard).

I could get a non-technical manager to talk about singletons, visitors, decorators, etc., in a few dull minutes, but he wouldn't actually know what he's talking about. I'd rather that the common vocabulary between developers was the actual problems they were trying to solve, rather than prefabricated boilerplate that constrains thought.

That said, I appreciate that patterns are a good learning tool, it's just a shame that learning sometimes stops there.

If this post isn't thought through that well, it's because my battery's nearly dead!
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Originally posted by Ricky Clarkson:
but he wouldn't actually know what he's talking about.

That's because he is using the pattern names as buzzwords. The abstraction provided by design patterns only reaches its true value between parties who have the same in-depth understanding about what these patterns are all about. That is one problem with novices as they will over- or misapply patterns to "show off" their prowess, when in fact they are obfuscating the issue.

Gregg Irwin's ten stages of design pattern adoption:
  • You use it without being aware that you're using it
  • You hear about it, read up on it, and tinker a bit
  • You learn more and start using it explicitly, if na�vely
  • You get the fire and evangelize (optional)
  • Something "clicks"
  • You learn more and apply it "less na�vely" and more implicitly
  • Time passes and you see flaws
  • You question the concept (often because you misapplied it)
  • You either forget about it or add knowledge and experience (Repeat steps 5�9 as necessary)
  • You use it without being aware that you're using it"



  • as quoted in Applying Domain Driven Design and Patterns (amazon US; Article: A Head Start on Domain-Driven Design Patterns)
    Ilja Preuss
    author
    Sheriff

    Joined: Jul 11, 2001
    Posts: 14112
    Originally posted by Ricky Clarkson:
    I haven't seen the relevant passage from GoF. I based my assertions on the wikipedia article.


    Then see http://en.wikipedia.org/wiki/Talk:Singleton_pattern#Global_point_of_access_for_the_singleton_instance


    Hence my assertion that a singleton is not an elaborate global variable, but merely a class that is instantiated exactly once.


    I see.

    In my experience, it's generally not wise to *fully* trust wikipedia articles.

    I can't say that I understood what you were getting at with the rest of your post, sorry.
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    I have to back up a bit from my lazy position before. The value of using the pattern name at all is to communicate a fair amount of information in a short space. When you misuse the name you miscommunicate, i.e. the listener who knows the pattern won't hear what you think you said, which makes what you said "wrong" from his end of the conversation. (Sorry, just watched House, might be taking on his tone.)

    I make a point when I wander off with unofficial factory patterns - because I think GOF is a bit too fussy for me - to point out the "lower case f". Cooper's Singleton simply isn't a GOF Singleton because it doesn't provide the well known access point. It's more the Just Create One that Ilja suggested, with seriously unfriendly enforcement.
     
    Consider Paul's rocket mass heater.
     
    subject: simplest singleton