设计模式12:组合模式

组合模式(Composite DP)允许将对象组合成树形结构来表现“整体-部分”层次结构。组合能让客户以一致的方式处理个别对象以及对象组合(原文:Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly)。
这个模式在安卓里面也有,就是View的层次结构。ViewGroup可以作为View的容器,本身也是View,因此可以形成树形结构。
这个模式的好处在哪?个人感觉就是能把一个复杂的树状结构相对简化,方便处理。

代码:
现在是一个字母、单词和句子的组合。
先定义组合模式的基类:

/**
 * 
 * Composite interface.
 * 
 */
public abstract class LetterComposite {

  private List children = new ArrayList<>();

  public void add(LetterComposite letter) {
    children.add(letter);
  }

  public int count() {
    return children.size();
  }

  protected abstract void printThisBefore();

  protected abstract void printThisAfter();

  /**
   * Print
   */
  public void print() {
    printThisBefore();
    for (LetterComposite letter : children) {
      letter.print();
    }
    printThisAfter();
  }
}

字母:

/**
 *
 * Letter
 *
 */
public class Letter extends LetterComposite {

  private char c;

  public Letter(char c) {
    this.c = c;
  }

  @Override
  protected void printThisBefore() {
    System.out.print(c);
  }

  @Override
  protected void printThisAfter() {
    // nop
  }
}

单词:

/**
 *
 * Word
 *
 */
public class Word extends LetterComposite {

  /**
   * Constructor
   */
  public Word(List letters) {
    for (Letter l : letters) {
      this.add(l);
    }
  }

  @Override
  protected void printThisBefore() {
    System.out.print(" ");
  }

  @Override
  protected void printThisAfter() {
    // nop
  }
}

句子:

/**
 *
 * Sentence
 *
 */
public class Sentence extends LetterComposite {

  /**
   * Constructor
   */
  public Sentence(List words) {
    for (Word w : words) {
      this.add(w);
    }
  }

  @Override
  protected void printThisBefore() {
    // nop
  }

  @Override
  protected void printThisAfter() {
    System.out.print(".");
  }
}

模拟信息:

/**
 * 
 * Messenger
 *
 */
public class Messenger {

  LetterComposite messageFromOrcs() {

    List words = new ArrayList<>();

    words.add(new Word(Arrays.asList(new Letter('W'), new Letter('h'), new Letter('e'), new Letter(
        'r'), new Letter('e'))));
    words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'), new Letter('e'), new Letter(
        'r'), new Letter('e'))));
    words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
    words.add(new Word(Arrays.asList(new Letter('a'))));
    words.add(new Word(Arrays.asList(new Letter('w'), new Letter('h'), new Letter('i'), new Letter(
        'p'))));
    words.add(new Word(Arrays.asList(new Letter('t'), new Letter('h'), new Letter('e'), new Letter(
        'r'), new Letter('e'))));
    words.add(new Word(Arrays.asList(new Letter('i'), new Letter('s'))));
    words.add(new Word(Arrays.asList(new Letter('a'))));
    words.add(new Word(Arrays.asList(new Letter('w'), new Letter('a'), new Letter('y'))));

    return new Sentence(words);

  }

  LetterComposite messageFromElves() {

    List words = new ArrayList<>();

    words.add(new Word(Arrays.asList(new Letter('M'), new Letter('u'), new Letter('c'), new Letter(
        'h'))));
    words.add(new Word(Arrays.asList(new Letter('w'), new Letter('i'), new Letter('n'), new Letter(
        'd'))));
    words.add(new Word(Arrays.asList(new Letter('p'), new Letter('o'), new Letter('u'), new Letter(
        'r'), new Letter('s'))));
    words.add(new Word(Arrays.asList(new Letter('f'), new Letter('r'), new Letter('o'), new Letter(
        'm'))));
    words.add(new Word(Arrays.asList(new Letter('y'), new Letter('o'), new Letter('u'), new Letter(
        'r'))));
    words.add(new Word(Arrays.asList(new Letter('m'), new Letter('o'), new Letter('u'), new Letter(
        't'), new Letter('h'))));

    return new Sentence(words);

  }

}

调用print就会挨个调用子组合的print从而打印整个句子。

你可能感兴趣的:(设计模式12:组合模式)