本文實例代碼:github.com/JamesZBL/ja…java
組合(Composite)模式用於將對象組合成樹形結構以表示 「部分——總體」 的層次結構。它使得用戶對單個對象和組合對象的使用具備一致性。git
在中文中,一句話是由詞語組成的,而詞語又由字組成;在英文中,句子由單詞組成,而單詞又由一個個字母組成。每一個對象均可定義的它以前的或以後的內容。好比一箇中文句子老是以句號結尾,一個英文單詞以前一般是有空格的。這種結構能夠造成了遞歸嵌套的結構,句子是父容器,單詞是子容器,字母是葉節點。 github
CharacterComposite 是一個抽象類,定義了全部容器類或葉節點的接口,容器應當實現的功能有:獲取子組件、對子組件進行計數、定義組件的格式化輸出規則。Sentence(句子) 和 Word (單詞)都屬於容器,而 Character (字母)則屬於葉節點,由於字母中沒法再添加子組件了,它是層次結構中的最末端。/** * 全部容器的抽象父類 */
public abstract class CharacterComposite {
private List<CharacterComposite> children = new ArrayList<>();
public void add(CharacterComposite character) {
children.add(character);
}
public int count() {
return this.children.size();
}
public void printBefore() {
}
public void printAfter() {
}
public void print() {
printBefore();
for (CharacterComposite item : children) {
item.print();
}
printAfter();
}
}
複製代碼
EnglishWord 組件前應當輸出一個空格,EnglishSentence 組件後應當輸出一個「.」,ChineseSentence 組件後應當輸出一個「。」等。ide
/** * 英文句子 */
public class EnglishSentence extends CharacterComposite {
public EnglishSentence(List<EnglishWord> words) {
for (EnglishWord word : words) {
add(word);
}
}
@Override
public void printAfter() {
System.out.println(".");
}
}
複製代碼
/** * 英文單詞 */
public class EnglishWord extends CharacterComposite {
public EnglishWord(List<Character> characters) {
for (Character c : characters) {
add(c);
}
}
@Override
public void printBefore() {
System.out.print(" ");
}
}
複製代碼
Word 做爲 Sentence 的子容器,Character 做爲 Word 的子組件,屬於葉節點。this
/** * 字母 */
public class Character extends CharacterComposite {
private char c;
public Character(char c) {
this.c = c;
}
@Override
public void print() {
System.out.print(c);
}
}
複製代碼
Writer 爲句子生成器,各個組件及子組件均由它負責填充,最終造成一個完成的結構。spa
/** * 語句生成器 */
public class Writer {
public CharacterComposite sentenceByChinese() {
List<ChineseWord> words = new ArrayList<>();
words.add(new ChineseWord(Arrays.asList(new Character('我'))));
words.add(new ChineseWord(Arrays.asList(new Character('是'))));
words.add(new ChineseWord(Arrays.asList(new Character('來'), new Character('自'))));
words.add(new ChineseWord(Arrays.asList(new Character('北'), new Character('京'))));
words.add(new ChineseWord(Arrays.asList(new Character('的'))));
words.add(new ChineseWord(Arrays.asList(new Character('小'), new Character('明'))));
return new ChineseSentence(words);
}
public CharacterComposite sentenceByEnglish() {
List<EnglishWord> words = new ArrayList<>();
words.add(new EnglishWord(Arrays.asList(new Character('I'))));
words.add(new EnglishWord(Arrays.asList(new Character('a'), new Character('m'))));
words.add(new EnglishWord(Arrays.asList(new Character('a'))));
words.add(new EnglishWord(Arrays.asList(new Character('s'), new Character('t'), new Character('u'), new Character('d'), new Character('e'), new Character('n'), new Character('t'))));
words.add(new EnglishWord(Arrays.asList(new Character('f'), new Character('r'), new Character('o'), new Character('m'))));
words.add(new EnglishWord(Arrays.asList(new Character('L'), new Character('o'), new Character('n'), new Character('d'), new Character('o'), new Character('n'))));
return new EnglishSentence(words);
}
}
複製代碼
我的博客同步更新,獲取更多技術分享請關注:鄭保樂的博客3d