Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Singleton vs. static class

Name: Anonymous 2008-05-07 20:13

It seems all the object-oriented design guidelines say that if you want only one instance of a class to exist at any time, you should use a singleton. My question is, what are the advantages of using a singleton over simply using a class where all the member variables and methods are static?

Name: Anonymous 2008-05-07 20:21

>>1
Why would you want only one instance of a class to exist? Sounds like a "design pattern" that's just meant to work around OBJECT OBSESSION.

Name: Anonymous 2008-05-07 20:23

"Global variable" would make a very poor name for a pattern.
And also some shit about being more adaptable when it turns out you really needed more than one, but then you'll be rewriting a lot of shit anyway.

Name: Anonymous 2008-05-07 20:23

Stop doing that.

Name: Anonymous 2008-05-07 20:24

In C++, you don't easily control the creation order of statics.  It happens to initialize in whatever order the linker put them in.  If they're initialized on first access like a singleton does, you can be sure that other stuff it depends on is initialized before it and you're not hitting segfaults before main().

In Java and other modern languages, there is no difference as the classloader initializes it in the proper order.

One other supposed advantage is that you can change the interface to a singleton much easier, or if you pass around the singleton then you can actually branch out to using multiple objects later on.  However, since all singletons are access by Foo.instance() directly, you never bother passing them around anyway.

Name: Anonymous 2008-05-07 20:24

That's basically my question. Why use a singleton, as opposed to using a class that's entirely static, which is basically just a procedural component?

Name: Anonymous 2008-05-07 20:26

SINGLETON CCLASSES CAN SUPPORT STATE. I.E. U CAN HAVE VARIABLES IN THEM; WHICH U CAN SAVE FOR LATER EXECUTION PERIODS

Name: Anonymous 2008-05-07 20:27

sorry my caps lock was one

Name: Anonymous 2008-05-07 20:33

But you can have static member variables too. Some people also say that you really shouldn't have non-constant static members, but I don't see why not. Using static member variables would mean your static class supports state too, so you don't have to use a singleton for that.

Name: Anonymous 2008-05-07 20:37

>>3
this.
Static classes don't offer any improvements and if you need to change something then you'll have to do a lot of backtracking.

Name: Anonymous 2008-05-07 20:41

Did you read >>5 ?
C++ : you'd better use singletons or you'll risk weird crashing on startup
Java, etc : no difference whatsoever

Name: Anonymous 2008-05-07 20:45

>>1
DON'T USE SINGLETONS!!!
Design patterns fucking suck, and you have no excuse to use them now that even enterprise object-oriented languages like C# are including lambdas.
Seriously though, even Micro$oft gets away with using static classes instead of singletons. I mean, take a look at XNA (it may not be the pinnacle of good design, but its easy as fuck to use).

Name: Anonymous 2008-05-07 20:45

So what's with the stateful vs. stateless argument? What makes static classes stateless, when they can hold variables just fine? Is it some old convention that declares that you shouldn't have static classes hold state, even though nothing prevents them from doing so?

Name: Anonymous 2008-05-07 20:50

>>13
You've got to understand that anything pertaining to C++-style OO is the work of morons trying to seem smart. These languages are their little intellectual ghetto.

Name: Anonymous 2008-05-07 20:59

>>14
These languages are their little intellectual ghetto.
hahahha oh wow.

Name: Anonymous 2008-05-08 12:52

One advantage of singletons is that (if you write them properly), they won't be instantiated until necessary. And you can let them be garbage collected (after writing the state to disk).

Personally I don't use them, nor do I use static variables, because global state sucks. Just pass any required objects in through the constructors, etc.

Name: Anonymous 2008-05-08 14:22

Static classes doesn't have constructors. Assume you have to run a loop or read from a file (or some silly shit like that) in order to initialize your class. Static would need a workaround to function that way (initialize every time you call a method or check whether the class has been initialized). In a singleton pattern you may do it in the constructor.

Name: Anonymous 2009-03-06 7:15

Don't want to however   spend large amounts   of connections so   it basically shat.

Name: Anonymous 2011-02-04 16:09

Name: Sgt.Kabukiman욏훂 2012-05-23 5:26

All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List