Holding reference to a singleton via static variable?

I

iksrazal

I have a singleton that works well. I have serveral classes that uses
this singleton to preform many operations. I want to break up these
many operations into methods for each class. My idea is:

class useSingleton1
{
private static MySingleton _instance = MySingleton.getInstance();

public void doWork()
{
doTask1();
doTask2();
}

public void doTask1()
{
// setup task1
// ...
_instance.doSomething1();
}

public void doTask2()
{
// setup task2
// ...
_instance.doSomething2();
}
}

This would be repeated in useSingleton2, etc. One potential caveat:
The singleton does Thread work, but that is working fine as it is now
- doing:

class useSingletonAsIsNow1
{
public void doWork()
{
MySingleton _instance = MySingleton.getInstance();
// setup task1
// ...
_instance.doSomething1();
// setup task1
// ...
_instance.doSomething2();
}
}

I'm currently just repeating for useSingletonAsIsNow2, etc, but as I
said I'd like to break up the code into methods.

I could do:

doTask1(_instance);
doTask2(_instance);

but it seems ugly to me.

Any ideas on the best approach?
iksrazal
 

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
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top