|
| 1 | +--- |
| 2 | +layout: pattern |
| 3 | +title: Composite |
| 4 | +folder: composite |
| 5 | +permalink: /patterns/composite/ |
| 6 | +categories: Structural |
| 7 | +tags: |
| 8 | + - Gang of Four |
| 9 | +--- |
| 10 | + |
| 11 | +## 目的 |
| 12 | + |
| 13 | +将对象组合成树结构以表示部分整体层次结构。 组合可以使客户统一对待单个对象和组合对象。 |
| 14 | + |
| 15 | +## 解释 |
| 16 | + |
| 17 | +真实世界例子 |
| 18 | + |
| 19 | +> 每个句子由单词组成,单词又由字符组成。这些对象中的每一个都是可打印的,它们可以在它们之前或之后打印一些内容,例如句子始终以句号结尾,单词始终在其前面有空格。 |
| 20 | +
|
| 21 | +通俗的说 |
| 22 | + |
| 23 | +> 组合模式使客户能够以统一的方式对待各个对象。 |
| 24 | +
|
| 25 | +维基百科说 |
| 26 | + |
| 27 | +> 在软件工程中,组合模式是一种分区设计模式。组合模式中,一组对象将像一个对象的单独实例一样被对待。组合的目的是将对象“组成”树状结构,以表示部分整体层次结构。实现组合模式可使客户统一对待单个对象和组合对象。 |
| 28 | +
|
| 29 | +**程序示例** |
| 30 | + |
| 31 | +使用上面的句子例子。 这里我们有基类`LetterComposite`和不同的可打印类型`Letter`,`Word`和`Sentence`。 |
| 32 | + |
| 33 | +```java |
| 34 | +public abstract class LetterComposite { |
| 35 | + |
| 36 | + private final List<LetterComposite> children = new ArrayList<>(); |
| 37 | + |
| 38 | + public void add(LetterComposite letter) { |
| 39 | + children.add(letter); |
| 40 | + } |
| 41 | + |
| 42 | + public int count() { |
| 43 | + return children.size(); |
| 44 | + } |
| 45 | + |
| 46 | + protected void printThisBefore() { |
| 47 | + } |
| 48 | + |
| 49 | + protected void printThisAfter() { |
| 50 | + } |
| 51 | + |
| 52 | + public void print() { |
| 53 | + printThisBefore(); |
| 54 | + children.forEach(LetterComposite::print); |
| 55 | + printThisAfter(); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +public class Letter extends LetterComposite { |
| 60 | + |
| 61 | + private final char character; |
| 62 | + |
| 63 | + public Letter(char c) { |
| 64 | + this.character = c; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + protected void printThisBefore() { |
| 69 | + System.out.print(character); |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +public class Word extends LetterComposite { |
| 74 | + |
| 75 | + public Word(List<Letter> letters) { |
| 76 | + letters.forEach(this::add); |
| 77 | + } |
| 78 | + |
| 79 | + public Word(char... letters) { |
| 80 | + for (char letter : letters) { |
| 81 | + this.add(new Letter(letter)); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + protected void printThisBefore() { |
| 87 | + System.out.print(" "); |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +public class Sentence extends LetterComposite { |
| 92 | + |
| 93 | + public Sentence(List<Word> words) { |
| 94 | + words.forEach(this::add); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + protected void printThisAfter() { |
| 99 | + System.out.print("."); |
| 100 | + } |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +然后我们有一个消息携带者来携带消息。 |
| 105 | + |
| 106 | +```java |
| 107 | +public class Messenger { |
| 108 | + |
| 109 | + LetterComposite messageFromOrcs() { |
| 110 | + |
| 111 | + var words = List.of( |
| 112 | + new Word('W', 'h', 'e', 'r', 'e'), |
| 113 | + new Word('t', 'h', 'e', 'r', 'e'), |
| 114 | + new Word('i', 's'), |
| 115 | + new Word('a'), |
| 116 | + new Word('w', 'h', 'i', 'p'), |
| 117 | + new Word('t', 'h', 'e', 'r', 'e'), |
| 118 | + new Word('i', 's'), |
| 119 | + new Word('a'), |
| 120 | + new Word('w', 'a', 'y') |
| 121 | + ); |
| 122 | + |
| 123 | + return new Sentence(words); |
| 124 | + |
| 125 | + } |
| 126 | + |
| 127 | + LetterComposite messageFromElves() { |
| 128 | + |
| 129 | + var words = List.of( |
| 130 | + new Word('M', 'u', 'c', 'h'), |
| 131 | + new Word('w', 'i', 'n', 'd'), |
| 132 | + new Word('p', 'o', 'u', 'r', 's'), |
| 133 | + new Word('f', 'r', 'o', 'm'), |
| 134 | + new Word('y', 'o', 'u', 'r'), |
| 135 | + new Word('m', 'o', 'u', 't', 'h') |
| 136 | + ); |
| 137 | + |
| 138 | + return new Sentence(words); |
| 139 | + |
| 140 | + } |
| 141 | + |
| 142 | +} |
| 143 | +``` |
| 144 | + |
| 145 | +然后它可以这样使用: |
| 146 | + |
| 147 | +```java |
| 148 | +var orcMessage = new Messenger().messageFromOrcs(); |
| 149 | +orcMessage.print(); // Where there is a whip there is a way. |
| 150 | +var elfMessage = new Messenger().messageFromElves(); |
| 151 | +elfMessage.print(); // Much wind pours from your mouth. |
| 152 | +``` |
| 153 | + |
| 154 | +## 类图 |
| 155 | + |
| 156 | + |
| 157 | + |
| 158 | +## 适用性 |
| 159 | + |
| 160 | +使用组合模式当 |
| 161 | + |
| 162 | +* 你想要表示对象的整体层次结构 |
| 163 | +* 你希望客户能够忽略组合对象和单个对象之间的差异。 客户将统一对待组合结构中的所有对象。 |
| 164 | + |
| 165 | +## 真实世界例子 |
| 166 | + |
| 167 | +* [java.awt.Container](http://docs.oracle.com/javase/8/docs/api/java/awt/Container.html) and [java.awt.Component](http://docs.oracle.com/javase/8/docs/api/java/awt/Component.html) |
| 168 | +* [Apache Wicket](https://github.com/apache/wicket) component tree, see [Component](https://github.com/apache/wicket/blob/91e154702ab1ff3481ef6cbb04c6044814b7e130/wicket-core/src/main/java/org/apache/wicket/Component.java) and [MarkupContainer](https://github.com/apache/wicket/blob/b60ec64d0b50a611a9549809c9ab216f0ffa3ae3/wicket-core/src/main/java/org/apache/wicket/MarkupContainer.java) |
| 169 | + |
| 170 | +## 鸣谢 |
| 171 | + |
| 172 | +* [Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59) |
| 173 | +* [Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b) |
| 174 | +* [Refactoring to Patterns](https://www.amazon.com/gp/product/0321213351/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321213351&linkCode=as2&tag=javadesignpat-20&linkId=2a76fcb387234bc71b1c61150b3cc3a7) |
0 commit comments