Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Referencing an object

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I have a problem quite similar to the previous post.
I have a class - RecruiterAgent who creates an object of class JobDB.

public class RecruiterAgent
{
JobDB jdb;

RecruiterAgent()
{
jdb = new JobDB();
}
}


The JobDB class is a HashMap where I enter details.
public class JobDB
{
public HashMap hm;

public JobDB()
{
hm = new HashMap();
}

public void makeEntry()
{
//Enter some keys and values into the HashMap.
}
}

Now I have a class ManagerAgent, who wants to use the JobDB object, infact, the HashMap in particular. This Manager Agent needs to make searches on the HashMap.

My application must have only 1 HashMap which has all the details.

How do I access the HashMap from my ManagerAgent?
 
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The RecruiterAgent shouldn't create the JobDB. A fourth class can assemble the whole application:

JobDB jobs = new JobDB();
RecruiterAgent ra = new RecruiterAgent(jobs);
ManagerAgent ma = new ManagerAgent(jobs);

Now, neither agent should try to get ahold of the HashMap -- JobDB should have methods they can call to do their searches. The HashMap member variable should be private, of course.
 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, thanks for thr reply. My application is a complex one in JADE where I am developing a multi agent system. So, I can't really create a new class that binds everyhting togther (although its the ideal way to do it).

But how do I solve my problem?

Class RecruitmentOfficer creates an object of JobsDB. JobsDB has a hashmap.(the only hashmap for the whle application).

OperationsManager agent wants to access this hashmap and make some searches in it. How can OperationsManager get a hold of the Hashmap?

Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24203
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, although I think the Singleton pattern is overused, that's really what you're asking for here. If there's ever only supposed to be one JobsDB, then make it a static member of its own class, and put a static method in the JobsDB class which retrieves that one object. Now, you said another class creates that one instance, but perhaps you could let the JobsDB class create it itself?



Now any class that wants to get ahold of that one JobsDB instance just has to call "JobsDB.get()" to get it. JobsDB is a "Singleton class."
 
Lucky Singh
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thats interesting. I did study the Singleton pattern but this thought never occurred to me.
Thanks a lot. I'll try it out right away.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic