从另一个类访问一个类中的列表的最佳方法

发布于 2024-12-27 08:21:35 字数 320 浏览 1 评论 0原文

我有一个类,我们称之为“Words”,它读取文件并创建字符串列表。

然后,我有一个类“Items”,它创建“描述”对象的列表,每个描述对象都需要从“Words”访问该列表。

由于“Word”列表读取文件,我显然不想为每个“描述”对象创建该列表。那么访问该列表的最佳方式是什么?

我是否应该从“Words”创建一个函数 getList() 并将其传递给“Items”构造函数,然后再次将其传递给每个“描述”?或者有更好的方法吗?如果我这样做,那么我还想确保它只是对列表的引用,而不是副本,因为“单词”列表可能会变得很大。

我对java比较陌生,任何帮助将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栖迟 2025-01-03 08:21:35

我会尽力抵制传递该单词列表的诱惑。我在那里没有看到任何封装。

我可能会为初始化和管理单词列表的类提供一个方法,该方法将采用一个 Item 和一个接口,该接口将显示如何填充或过滤给定 Item 的单词列表。

我猜测与某个项目相关的单词数量只是较大整体的一小部分,并且项目数量是可以管理的。

我只是想看看你没有将对象变成愚蠢的结构或数据传输对象,从而揭示有关其内部状态的所有信息。如果可以的话,将行为封装在对象内并隐藏细节和复杂性。该级别的客户会感谢您。

更新:根据您下面的评论,我想知道关系数据库是否是您真正需要的。一个项目需要一个描述列表;它是关系数据库中的简单联接并映射到对象。

解析和填充表是一次性的事情。您的 Java 应用程序只能查询具有给定描述的 Item 实例。例如,您可以要求它告诉您所有具有描述“foo”的项目。使用内存中的 Java 对象可能会很费力且效率低下。让关系优化器为您加速。这样,您也不必同时将所有对象都存储在内存中。只需查询您需要的内容即可。

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.

赤濁 2025-01-03 08:21:35

您需要创建一个类来填充 String 对象(单例)的 List,在描述类的构造函数中调用该 singleTon 方法,将列表的引用分配给 描述对象。

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;
    }
}

在描述类中,

public class Description
{
   private String desc;
   private ArrayList<String> words;

   public Description(String desc) 
    {
        this.desc=desc; 
        this.words=Words.getList();
     }
}

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();
     }
}
两个我 2025-01-03 08:21:35

您有两种选择:

  • 您可以按照您所说的使用 getList() 从 Words 检索列表。然后,您可以通过其构造函数将该列表传递到新的描述对象中。
  • 您可以将 Words 中的列表声明为 public static。这将允许您通过类引用来引用列表; Words.list;

第一个解决方案可能是您的最佳选择,因为声明静态变量通常是不可取的。

You have two choices:

  • You can retrieve the list from Words using getList() as you said. You then pass the list into the new description object via its constructor.
  • You can declare the List in Words as 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.

2025-01-03 08:21:35

我想做如下:

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 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文