Reputation: 53
I have a class, lets call it 'Words', that reads a file and creates a list of strings.
I then have a class, 'Items', that creates a list of 'description' objects, each description object needs access to the list from 'Words'.
Since the 'Word' list reads a file, I obviously don't want to create that list for each 'description' object. So what would be the best way of accessing that list?
Should I just create a function getList() from 'Words' and pass it to 'Items' constructor, then pass it again to each 'description'? or is there a better way? If I do that, then I would also want to make sure it is only a reference to the list and not a copy since the 'Words' list can get huge.
I am relatively new to java and any help would be appreciated.
Upvotes: 3
Views: 8749
Reputation: 308763
I would try to resist the temptation to pass that List of words around. I don't see any encapsulation there.
I might give the class that initializes and manages the word list a method that would take an Item and an interface that would show how to populate or filter that word list for a given Item.
I'm guessing that the number of words associated with an item is a small subset of the larger whole, and the number of items is manageable.
I'd just want to see that you didn't turn objects into dumb structs or data transfer objects that revealed everything there was to know about their internal state. If you can, encapsulate behavior inside an object and hide details and complexity. Clients of that class will thank you.
UPDATE: Based on your comment below, I'd wonder if a relational database is what you really need. An Item needs a List of Descriptions; it's a simple JOIN in a relational database and mapping to objects.
Parsing and populating the tables is a one-time thing. Your Java application can just query for Item instances that have given Descriptions. You can ask it to tell you all the Items that have Description "foo", for example. That could be laborious and inefficient using an in-memory Java object. Let the relational optimizer speed it up for you. You don't have to have all the objects in memory at the same time that way, either. Just query for what you need.
Upvotes: 4
Reputation: 11
I would like to do as below:
public class Words{
private static List words;
private Words();
public static List getInstance(){
if(words == null){
words = getFile();
}
return words;
}
private List getFile(){
//get file
}
}
public class Items{
public List items = Words.getInstance();
}
I'm sorry I haven't tested this code, hopes it will help you to think out a better way if it's wrong.
Upvotes: 0
Reputation: 94645
You need to create a class that pupulate the List
of String object (singleton), call that singleTon method in constructor of description class to assign reference of list to the description
object.
public class Words
{
private static ArrayList<String> words;
public static ArrayList<String> getWords()
{
if(words==null)
{
words=new ArrayList<String>();
//read strings from the file and add them into list
}
return words;
}
}
In description class,
public class Description
{
private String desc;
private ArrayList<String> words;
public Description(String desc)
{
this.desc=desc;
this.words=Words.getList();
}
}
Upvotes: 2
Reputation: 23250
You have two choices:
public static
. This will allow you to reference the List by class reference; Words.list;
The first solution is probably your best option, as declaring static variables is usually undesirable.
Upvotes: 0