File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
Author

Whats a Singleton alternative ?

salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

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 ?

My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

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: http://aspose.com/file-tools
 
subject: Whats a Singleton alternative ?