Configure a singleton

S

ShadowMan

Hi all,
according to your opionion, what is the best way to configure a Singleton
through an external fileName.
I have two solutions:
- using a static method that configures a Singleton and return its instance
Example: Singleton singleton = Singleton.init(fileName);

- use a public (not static) method to call directly on instance

Example:
Singleton singleton = Singleton.getInstance();
singleton.init(fileName)

I have to use it on a clustered environment..
 
F

Ferenc Hechler

I think the correct way is to do the initialization in the getInstance()
method (only at first call and synchronized).
What do you mean with clustered environment?
bye,
feri
 
F

Ferenc Hechler

OK,
my first answer did not match your question: How to pass some
config-file-data to a class containing a singleton.
For example Log4J uses a default-configuration file "log4j.properties" or
so, which is searched in the classpath.
But you can change the configuration by explicitly setting the properties
from another configuration file (PropertyConfigurator).

I think this is a flexible way. Add a method setConfig(...) which overwrites
some defaults which are used otherwise.

bye,
feri

Ferenc Hechler said:
I think the correct way is to do the initialization in the getInstance()
method (only at first call and synchronized).
What do you mean with clustered environment?
bye,
feri
 
S

ShadowMan

Ferenc Hechler said:
OK,
my first answer did not match your question: How to pass some
config-file-data to a class containing a singleton.
For example Log4J uses a default-configuration file
"log4j.properties" or so, which is searched in the classpath.
But you can change the configuration by explicitly setting the
properties from another configuration file (PropertyConfigurator).

I think this is a flexible way. Add a method setConfig(...) which
overwrites some defaults which are used otherwise.

perfect...so this method should be static!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top