Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Holding reference to a singleton via static variable?

Reply
Thread Tools

Holding reference to a singleton via static variable?

 
 
iksrazal
Guest
Posts: n/a
 
      08-26-2004
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to check what is holding reference to object Michal M Python 7 05-05-2010 07:47 PM
Destroy a Singleton : static or not static ? requinham C++ 11 02-19-2010 08:12 AM
Desctruction of objects inside a static nested map holding pointers jayaramganapathy@gmail.com C++ 12 11-27-2007 10:11 AM
Holding reference to a singleton via static variable? iksrazal Java 0 08-26-2004 02:46 PM
Holding a reference to a ruby object outside the interpreter Thomas Ruby 3 09-01-2003 06:31 PM



Advertisments