Welcome to Dream.In.Code
Become a Java Expert!

Join 150,373 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,780 people online right now. Registration is fast and FREE... Join Now!




Google's Singleton detector

2 Pages V  1 2 >  
Reply to this topicStart new topic

Google's Singleton detector, Singleton is bad

bhandari
13 Feb, 2008 - 02:12 AM
Post #1

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
Singleton pattern has been discouraged due to inavailability of scope. Here's more for reading.

Singleton Detector
User is offlineProfile CardPM
+Quote Post

letthecolorsrumble
RE: Google's Singleton Detector
13 Feb, 2008 - 02:32 AM
Post #2

Student of The Sun
Group Icon

Joined: 7 Nov, 2007
Posts: 550



Thanked: 1 times
My Contributions
Thanks...although I have no idea what a singleton is wink2.gif

This post has been edited by letthecolorsrumble: 13 Feb, 2008 - 02:46 AM
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Google's Singleton Detector
13 Feb, 2008 - 03:48 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,289



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
Totally disagree. If it weren't for Singletons, I'd have to see code with classes full of statics. Same effect, but statics are the greater evil.

To be on same page, this is the Singleton:
CODE

class Foo {
    // this is the one and only instance of Foo we'll ever use
    private static Foo instance = null;
    
    // This is how the world gets the "single" instance
    // using lazy init, because GoF love that
    public static Foo getInstance() {
        if (instance == null) { instance = new Foo(); }
        return instance;
    }
    
    // this prevents the world from making their own instance.
    private Foo() { }
}

class Bar {
    private Foo myFoo = null;
    public Bar() {
        // This will not work!  hah, Singleton
        //myFoo = new Foo();
        
        //this works, and every class does it this way.
        myFoo = Foo.getInstance();
    }
}


Basically, this solves that newbie question, how do my classes share values? It also answers the plea, I want globals in my OO design. This is meant to solve the worse evil of classes made up of static methods, which is what people would normally do if that wanted such behavior.

Found another rant about them here.

Personally, if I see any evidence of a design pattern in the wild, I'm thrilled. However, any design can be used poorly. Looks like at Google they use it very poorly. tongue.gif

User is offlineProfile CardPM
+Quote Post

bhandari
RE: Google's Singleton Detector
13 Feb, 2008 - 03:52 AM
Post #4

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
or they developed that detector for themselves and thought others might also be using it poorly.
User is offlineProfile CardPM
+Quote Post

1lacca
RE: Google's Singleton Detector
13 Feb, 2008 - 04:38 AM
Post #5

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 12 times
My Contributions
Guns don't kill people...



User is offlineProfile CardPM
+Quote Post

bhandari
RE: Google's Singleton Detector
14 Feb, 2008 - 05:41 AM
Post #6

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
Even bombs also don't kill people.
User is offlineProfile CardPM
+Quote Post

Nayana
RE: Google's Singleton Detector
14 Feb, 2008 - 05:54 AM
Post #7

DIC Hawk - 나야나 नयन:
Group Icon

Joined: 14 Nov, 2007
Posts: 824



Thanked: 5 times
Dream Kudos: 175
My Contributions
QUOTE(1lacca @ 13 Feb, 2008 - 05:38 AM) *

Guns don't kill people...


I beg to differ.

IPB Image
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Google's Singleton Detector
14 Feb, 2008 - 12:37 PM
Post #8

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,256



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
This has got to be an early April Fool's joke. Someone call the GoF guys. rolleyes.gif
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Google's Singleton Detector
14 Feb, 2008 - 12:52 PM
Post #9

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,256



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
QUOTE(baavgai @ 13 Feb, 2008 - 05:48 AM) *

Found another rant about them here.

Read this guys rather long and rambling article. You want the gist? Basically he's saying that when he was s newb he misused the Singleton. He also goes lists many "negatives" about the Singleton - all of which can be chalked up to bad design. He says the Singleton is basically just a memory leak. Sure it is...if you mis-use it like he did. He also complained about all of the boilerplate that went along with the Singleton. Maybe someone should introduce him to the concept of dependency injection. Hello? Dude...try Google Guice, or Springs JavaConfig.

So - to sum up.
1. Use Singleton sparingly. It's a great pattern when you actually need to use it.
2. If you have to use it, use dependency injection to decouple your classes (dependency inversion principle) and rid yourself of much of the boilerplate.
User is offlineProfile CardPM
+Quote Post

bhandari
RE: Google's Singleton Detector
15 Feb, 2008 - 12:20 AM
Post #10

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
Hi Programmist,

I have been looking for some good book/tutorial on dependency injection in Spring. Can u name one??
User is offlineProfile CardPM
+Quote Post

Programmist
RE: Google's Singleton Detector
15 Feb, 2008 - 07:58 AM
Post #11

Four-letter word
Group Icon

Joined: 2 Jan, 2006
Posts: 1,256



Thanked: 11 times
Dream Kudos: 100
Expert In: Java

My Contributions
QUOTE(bhandari @ 15 Feb, 2008 - 02:20 AM) *

Hi Programmist,

I have been looking for some good book/tutorial on dependency injection in Spring. Can u name one??

I don't know of any books on the subject (although there must be some), but you can find the documentation online in the link I provided in my last post.

Edit: This guy does an article comparing the differing DI styles of Guice and JavaConfig. His article has many facts, but is also full of opinions and biases since the site is about Spring and he has written books on Spring. At least he admits upfront that he's biased and is probably not as familiar with Guice as with Spring.

This post has been edited by Programmist: 15 Feb, 2008 - 08:02 AM
User is offlineProfile CardPM
+Quote Post

bhandari
RE: Google's Singleton Detector
15 Feb, 2008 - 08:06 AM
Post #12

D.I.C Addict
Group Icon

Joined: 31 Jan, 2008
Posts: 747


Dream Kudos: 900
My Contributions
that has proved to be of not much help. I have my project in my organization which is built using spring (some context thing).

Anyway thanks. Will try some book shop.
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:34PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month