File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Whats a Singleton alternative ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Whats a Singleton alternative ?" Watch "Whats a Singleton alternative ?" New topic

Whats a Singleton alternative ?

salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 1110

I have gone thru a couple of posts that said singleton is 'antipattern', blah,blah.

Honestly I do not believe so, but my curiosity has brought me to a simple question, whats a singleton alternative ?
At first I thought its a factory pattern...

Lets say we have a scenario:
A huge gaming application has a game engine thats accessible to all the subclasses.
Lets call it "GameEngine", now, ideally I would use a GameEngine.getInstance() synchronized method for a singleton implementation.

In the realm of the factory pattern, here is how I think sub classes will refer to it :

or would it be

wouldn't that be same as:

So, how exactly would we pass around a object globally using a factory ?

There are tons of other examples where smaller objects require access to a Main Logic / Engine / GUI object.

how then would an implementation of a Singleton alternative look like in this case ?

I agree. Here's the better link:

salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 1110

As a side note, though I do not find singleton to be an anti-pattern, I do believe that using singleton just for the heck of storing variables that are globally accessible is a bad idea.
I agree. Here's the link:
subject: Whats a Singleton alternative ?
It's not a secret anymore!