-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFlyweight.java
More file actions
44 lines (37 loc) · 1.07 KB
/
Flyweight.java
File metadata and controls
44 lines (37 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.*;
class Sentence {
List<WordToken> words = new ArrayList<>();
public Sentence(String plainText) {
for (String word : plainText.split(" ")) {
words.add(new WordToken(word));
}
}
public WordToken getWord(int index) {
return words.get(index);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
for (WordToken word : words) {
if (sb.length() > 0) {
sb.append(" ");
}
sb.append(word.capitalize ? word.value.toUpperCase() : word.value);
}
return sb.toString();
}
class WordToken {
public String value;
public boolean capitalize;
WordToken(String value) {
this.value = value;
}
}
}
class DemoFlyWeight {
public static void main(String[] args) {
Sentence sentence = new Sentence("hello world");
sentence.getWord(1).capitalize = true;
System.out.println(sentence);
}
}