Holding reference to a singleton via static variable?

Discussion in 'Java' started by iksrazal, Aug 26, 2004.

  1. iksrazal

    iksrazal Guest

    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
    iksrazal, Aug 26, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. iksrazal
    Replies:
    0
    Views:
    403
    iksrazal
    Aug 26, 2004
  2. Replies:
    12
    Views:
    716
    James Kanze
    Nov 27, 2007
  3. requinham
    Replies:
    11
    Views:
    2,231
    Michael Doubez
    Feb 19, 2010
  4. Michal M
    Replies:
    7
    Views:
    282
    Marius Gedminas
    May 5, 2010
  5. Thomas
    Replies:
    3
    Views:
    137
    Sean O'Dell
    Sep 1, 2003
Loading...

Share This Page