File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Referencing an object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Referencing an object" Watch "Referencing an object" New topic
Author

Referencing an object

Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
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?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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.


[Jess in Action][AskingGoodQuestions]
Lucky Singh
Ranch Hand

Joined: Jan 19, 2004
Posts: 125
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
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

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

Joined: Jan 19, 2004
Posts: 125
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Referencing an object
 
Similar Threads
Iterator methods for HashMap class
design pattern - Builder pattern
Hash Map question ...
Session Management Still weird
HashMap Construction with initial values