Perry Smith

Java Questions & Answers

Archive

Home / Java Q & A Archive Page 4 / Java Q & A Archive / Java Main Page

Accessing an Exsisting Running Object Member
Monday, 05-Apr-99 17:11:30

167.127.142.7 writes:

I would like to access a Hashtable that was created in an exsisting running instance. How can 50 other running clients access the same hashtable when they instantiate their object?

Duane


Response
Re: Accessing an Exsisting Running Object Member
Tuesday, 13-Apr-99 12:17:28

209.47.111.2 writes:

Correct me if I'm wrong, but this sounds like you want a single Hashtable that's global -- at least from the point of view of the these 50 clients.

I can think of 2 ways of doing this.
1) Create some complex controller class that ensures that the Hashtable is passed to every client
2) Use a Design Pattern. This one is called Singleton. (Read Design Patterns by Gamma et. al. for more info -- it's great stuff.)

In your particular case, I would create a class that extends Hashtable (call it GlobalTable for now.)

      Create a static private varible in this class:
      GlobalTable _instance = null;
Make the GlobalTable constructor private. To instantiate the object, use the following method:
      public static GlobalTable getInstance(){
      if( _instance == null ){
      _instance = GlobalTable();
      }
      return _instance;
      }
What this does is check if _instance has been initialised yet. If it has, it is returned. If it hasn't, it is privately initialised and returned.

Hope this helps
Sonal


Home / Java Q & A Archive Page 4 / Java Q & A Archive / Java Main Page