Hello all!
I thought I was finally getting good at this "Java" thing, and then someone suggests to me that I use a singleton and factory class. I'd never heard of these before so I set off googling it. Now, I get the gist of singleton and factory classes, but I still have some more questions.

Here's some background info:
For my own fun little project, I'm creating this program which will read in commands from the terminal and execute the appropriate commands, just like UNIX. So, I'm basically trying to implement some way of essentially having a dictionary which derives the meaning of each word, and then performs the correct actions on the correct objects in accordance with the user's wishes. This is where someone suggested I use a singleton factory class.

So, I understand that singletons can only be implemented once. Question: What is the necessity of this? If I don't want another class to create an instance of it, then I just won't program it so that it will. Also, can someone provide me with a simple example of how to implement it?

I'm a little more hazy on factory classes. Do they create instances of other classes depending on the parameters that are passed to them? Why is this necessary and how does that differ from other classes? An example of implementation would be appreciated.

Thanks for your help.

Edited 6 Years Ago by kendaop: n/a

To force a singleton I suppose you could simply use a getInstance method and refuse to return an instance if one already exists. I *think* you could enforce this by instantiating a private constructor (thus preventing anyone from calling it outside the class). By instantiating a private constructor, you are preventing the default constructor from being created by the compiler or whatever. Those are my thoughts, the wikipedia references the getInstance method, but as far as the private constructor, I'm just winging it, I've never actually had the need to make every constructor of a class private, so try it out yourself.

Also, sorry for not giving an example, but as I said, I've never used a singleton, so personally, I consider that design pattern completely unnecessary. Perhaps someone here can give an example that will change my mind.

Edited 6 Years Ago by BestJewSinceJC: n/a

If you want more information of design patterns it would be a good idea to refer to a book like HeadFirst. I am sure that googling also would result in a good number of sites giving details of all the design patterns out there.

The basic idea of singleton is to have a single instance of a class.

An example where I have seen singleton implemented (C++) was an engine where we needed only one instance to run at a time.

Edited 6 Years Ago by thomas_naveen: n/a

To force a singleton I suppose you could simply use a getInstance method and refuse to return an instance if one already exists. I *think* you could enforce this by instantiating a private constructor

Yes, that's the standard solution.
The static getInstance method uses the private constructor to create an instance the first time it's called, then just returns the same instance on all subsequent calls.
This is preferred to having all the variables & methods static because:
You don't instantiate or initialise anything until it's needed (eg database connection)
You always have the upward-compatible option of changing the impelemtation to multiple instances (eg parallel database connections) at a later time without breaking anything. (In which case this is a factory!)

Edited 6 Years Ago by JamesCherrill: n/a

This article has been dead for over six months. Start a new discussion instead.