Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

constructors and singletons 5

Status
Not open for further replies.

ruthie

Programmer
Jul 19, 2001
2
IL
Hi
I have two unrelated questions regarding good object oriented practice:

1. how much should i put in a constructor: is it good to do all my initializations in it? when is it good to use init functions?

2. is it okay to use a singleton design pattern, and then use -new- to get a pointer to it from different places in the code?

thanks
ruthie
 
Why do you need init when you have contructors? John Fill
1c.bmp


ivfmd@mail.md
 
In answer to question 1...there is no accepted standard as far as I am aware, it is a personal decision regarding personal preference and having readable code that can be followed easily by whoever has to debug/change it etc.

If you find yourself writing a bunch of code that has little to do with the code preceding it, then this is sometimes a good indicator that that code is a candidate for having its own function/method, but only from a readability point of view.

question 2...ummm!!!??? D'OH

Static Apnea - snorkelling in a swimming pool without the snorkel?
 
Re: question 2

My understanding of the Singleton design pattern is that you should use it when you need one, and only one, instance of a class (hence the name). Any kind of 'manager' class, like a window manager, thread manager or database connection pool, would be a good candidate for implementation as a Singleton.

A class implemented as a Singleton in Java would look something like the following:

public class Singleton
{
private static Singleton instance=null;
// other variables...

private Singleton()
{
// initialize variables
}

public static Singleton getInstance()
{
if (instance == null) instance = new Singleton();
return instance;
}
}

So in order to get a reference to the single instance of Singleton, you'd need to call the public getInstance() method. The constructor is private so other classes can't instantiate the Singleton class.

Hope this helps.
 
1. you can use an init method if you have common code for more that one constructor although if you leave all the basic default construction code in the default constructor, you can just call

this();

in any of your other constructors.

2. how u get a handle to your singleton is up to yourself. as above, you can use the getInstance(), which i use, or you can use the new keyowrd and check for an instance in your constructor. the only difference between the two is in the readability in the code. if you use getInstance, when people ready code using your objects they will know instantly thats its a singleton. when u use the new keyword, anything could be happening. so, for clarity sake, use getInstance
 
How can you use the 'new' operator if (all) the constructor(s) is (are) private?
 
ruthie :

If you are talking about Java Applets in question 1, then it is a good practice to use only 1 of them, which in most cases is the init() method. A very simple reason is because you are only able to get value from the html tags through the method getParameter() in the init() method. It doesn't work in the constructor.

Singleton <- I forgot what it is about....haha.... think it is time to revise abit...

Regards,
Leon
 
varocho:

Why would you want to make (all) the constructor(s) private? What's the logic behind this? Could you tell me? I am confused by your question. ~~~
&quot;In C++, only friends can touch your private parts.&quot; - Emmanuel Ackouoy
 
If you're implementing a class as a Singleton, the class' constructors cannot be public. If the constructors are public, then any other class can create an instance of the class just by using the 'new' operator. This defeats the purpose of implementing the class as a Singleton in the first place. So instead of directly instantiating the class, you use the static 'getInstance()' (or whatever the method name is) method to get a reference to the single instance of the class.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top