mardi 8 janvier 2019

Flyweight design pattern in java, what is the purpose?

I have the following code:

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

and test:

@Test
public void test() {
    Sentence s = new Sentence("alpha beta gamma");
    s.getWord(1).capitalize = true;
    assertEquals("alpha BETA gamma", s.toString());
}

My question is: what is the purpose of use flyweight pattern in that way?

Aucun commentaire:

Enregistrer un commentaire