By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,249 Members | 1,853 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,249 IT Pros & Developers. It's quick & easy.

THE SINGLETON PATTERN ?

P: n/a
Hello everyone, I have been researching the Singleton Pattern. Since the
singleton pattern uses a private constructor which in turn reduces
extendability, if you make the Singleton Polymorphic what sort of
problems/issues should be considered?

Also, I see that a singleton needs to be set up with certain data such as
file name, database URL etc. What issues are involved in this, and how
would you do this?

If someone knows about the Singleton pattern please reply. Thanks

Alicia
Jul 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Hi Alicia
1) I uses protected constructor when I use singleton pattern

2) You can read more about singleton here
http://www.javaworld.com/javaworld/j...-double_p.html,

--
With best reguards,
Andrey Dobrov

ext 1111
"Alicia Roberts" <al******@hotmail.com> wrote in message
news:Xn********************************@216.77.188 .18...
Hello everyone, I have been researching the Singleton Pattern. Since the
singleton pattern uses a private constructor which in turn reduces
extendability, if you make the Singleton Polymorphic what sort of
problems/issues should be considered?

Also, I see that a singleton needs to be set up with certain data such as
file name, database URL etc. What issues are involved in this, and how
would you do this?

If someone knows about the Singleton pattern please reply. Thanks

Alicia

Jul 17 '05 #2

P: n/a
Alicia Roberts wrote:
Hello everyone, I have been researching the Singleton Pattern. Since the
singleton pattern uses a private constructor which in turn reduces
extendability, if you make the Singleton Polymorphic what sort of
problems/issues should be considered?

Also, I see that a singleton needs to be set up with certain data such as
file name, database URL etc. What issues are involved in this, and how
would you do this?

If someone knows about the Singleton pattern please reply. Thanks

Alicia


Alicia,

It has been my experience that the singleton pattern is not really a
good idea. No matter how convinced you are that you will only ever need
one of these objects, you inevitably end up needing more.

Ray

--
XML is the programmer's duct tape.
Jul 17 '05 #3

P: n/a
Hello Alicia,

first if all, you can not inherite from a Singleton since the instance variable
is static and the java compiler uses static binding to resolve the symbol at
runtime. Try it yourself by running something like:

public class SingletonTest1 {

protected static SingletonTest1 instance;

protected static Singletons getInstance() {
if ( instance == null ) {
new SingletonTest1();
} else {
return instance;
}
}

public void method() {
System.out.println( "Do something useless here." );
}
}

public class SingletonTest2
extends SingletonTest1 {

public void method() {
System.out.println( "Do something usefull here." );
}
}
Calling: SingletonTest2.getInstance().method(); will not resolve in printing
"Doing someting usefull here", but "something useless"!
Second, if you need to initialize the instance with parameters encapsulate
all parameters into one object and hand it over to the getInstance( ... )
method.

In general, do not use the Singleton Pattern to easily make values or methods
global accessible. This can also be done with public and static methods/fields.
Only use it in cases you definitely need an object (not just some helper methods)
and want to guarantee there's one one instance available during runtime.

Kind Regards,
Lothar
Jul 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.