2

For example, let's assume that the following data structures all implement the mapping of Lemmas->PartsOfSpeech->ListOfWords (topic choice is arbitrary and has nothing to do with question):

Map<ArrayList<Lemma>, Map<PartOfSpeech, ArrayList<Word>>> mainMap = new HashMap<>();

Map<ArrayList<Lemma>, PartOfSpeechWordMap> mainMap = new HashMap<>();

LemmaMap mainMap = new LemmaMap();

Where PartOfSpeechWordMap contains and manipulates Map<PartOfSpeech, ArrayList<Word>>, and LemmaMap contains and manipulates one of the above structures.

The list goes from most transparent and complicated, to least transparent and simplest; however, I'm not sure which is best from an object oriented perspective. As you go down the list, it gets more and more difficult to figure out what's going on, despite being more concise and having simpler syntax.

I understand that this question may be a little subjective, but I'd like to know if there are standard industry practices when dealing with something like this.

EDIT:
I wasn't thinking and didn't consider the issue with mutable keys. Let's assume that I'm using ImmutableList per Guava. Nonetheless, the point of my question was which structure makes most sense from an OOP perspective with regards to the varying levels of transparency.

4

2 回答 2

2

我会创建一个

class LemmaMap implements Map<List<Lemma>, Map<PartOfSpeech, List<Word>>>

whereLemmaMap包装 a HashMap,但也提供了一些额外的方法,例如

public List<Word> getAllWords(List<Lemma> key) {
    List<Word> returnValue = new ArrayList<>();
    for(List<Word> list : map.get(key).values()) {
        returnValue.addAll(list);
    }
    return returnValue;
}

这提供了透明度,同时也对调用者隐藏了映射,例如只想要与键关联的所有单词的列表而不关心如何从底层映射中获取它

于 2013-07-02T19:04:25.627 回答
1

我会说这不是这个问题的合适站点,但我相信大多数人会建议使用最简单的 API。这有助于减少不正确使用它的可能性(例如向键数组添加元素)。它还允许您在需要时更改底层实现细节。

于 2013-07-02T18:55:40.527 回答