Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flyweight设计模式在java中的作用是什么?_Java_Design Patterns - Fatal编程技术网

Flyweight设计模式在java中的作用是什么?

Flyweight设计模式在java中的作用是什么?,java,design-patterns,Java,Design Patterns,我有以下代码: public class Sentence { private String [] words; private Map<Integer, WordToken> tokens = new HashMap<>(); public Sentence(String plainText) { words = plainText.split(" "); } public WordToken getWord

我有以下代码:

public class Sentence {

    private String [] words;
    private Map<Integer, WordToken> tokens = new HashMap<>();

    public Sentence(String plainText) {
        words = plainText.split(" ");
    }

    public WordToken getWord(int index) {
        WordToken wt = new WordToken();
        tokens.put(index, wt);
        return tokens.get(index);
    }

    @Override
    public String toString() {
        List<String> ws = new ArrayList<>();
        for (int i = 0; i < words.length; ++i) {
            String w = words[i];
            if (tokens.containsKey(i) && tokens.get(i).capitalize) {
                w = w.toUpperCase();
            }
            ws.add(w);
        }
        return String.join(" ", ws);
    }
}
我的问题是:以这种方式使用flyweight模式的目的是什么?

是一种重用模式,通过重用相同的对象来减少程序的内存占用。这在表示简单值(如单词)的中很常见,因为具有相同字符的单词是相同的。例如,假设我们有以下句子(暂时忽略大写):

这个句子有39个字符,这意味着如果我们从这个句子中创建一个
字符串
,我们将需要存储39个字符(暂时忽略Java
字符串
实现使用的
长度
字段)。如果我们看这个句子,有3个
的实例,它们彼此相同。还有7个空格,它们彼此相同。如果我们对字符串进行标记,我们将获得以下单词:

["the", "doorman", "held", "the", "door", "for", "the", "guest"]
如果我们仅取此列表中的唯一值,我们将获得:

["the", "doorman", "held", "door", "for", "guest"]
使用这些独特的单词,我们可以通过将句子中单词的索引映射到独特的单词来创建句子:

[0, 1, 2, 0, 3, 4, 0, 5]
要重建句子,我们只需将上面的索引映射到唯一单词列表,在每个单词之间添加一个空格

在您提供的示例中,算法似乎不正确(它不节省任何空间,因为它同时存储单词和标记)。更正确的解决方案(众多解决方案中的一个)类似于:


我让您来实现
toString
方法。

为什么您认为此代码使用flyweight模式?在flyweight阵列中,共享同一对象以最小化内存使用。在这个类中,始终创建WordToken实例,因此没有对象共享,内存使用也没有最小化。
["the", "doorman", "held", "door", "for", "guest"]
[0, 1, 2, 0, 3, 4, 0, 5]
public class Sentence {

    private final List<Integer> wordMap = new ArrayList<>();
    private final List<String> words = new ArrayList<>();

    public Sentence(String sentence) {
        for (String word: sentence.split(" ")) {
            addIfNotExists(word);
            wordMap.add(words.indexOf(word));
        }
    }

    private void addIfNotExists(String word) {

        if (!words.contains(word)) {
            words.add(word);
        }
    }

    public List<Integer> getWordMap() {
        return wordMap;
    }

    public List<String> getWords() {
        return words;
    }

    public static void main(String[] args) {
        Sentence s = new Sentence("the doorman held the door for the guest");
        System.out.println(s.getWordMap());
        System.out.println(s.getWords());
    }
}
[0, 1, 2, 0, 3, 4, 0, 5]
[the, doorman, held, door, for, guest]