目的

将对象组合成树形结构来表示“整体/部分”层次关系,允许用户以相同的方式处理单独对象和组合对象。

类图

组件(Component)类是组合类(Composite)和叶子类(Leaf)的父类,可以把组合类看成是树的中间节点。
组合对象拥有一个或者多个组件对象,因此组合对象的操作可以委托给组件对象去处理,而组件对象可以是另一个组合对象或者叶子对象。
组合类图.png

实现

  1. public abstract class Component {
  2. protected String name;
  3. public Component(String name) {
  4. this.name = name;
  5. }
  6. public void print() {
  7. print(0);
  8. }
  9. abstract void print(int level);
  10. abstract public void add(Component component);
  11. abstract public void remove(Component component);
  12. }
  13. public class Composite extends Component {
  14. private List<Component> child;
  15. public Composite(String name) {
  16. super(name);
  17. child = new ArrayList<>();
  18. }
  19. @Override
  20. void print(int level) {
  21. for (int i = 0; i < level; i++) {
  22. System.out.print("--");
  23. }
  24. System.out.println("Composite:" + name);
  25. for (Component component : child) {
  26. component.print(level + 1);
  27. }
  28. }
  29. @Override
  30. public void add(Component component) {
  31. child.add(component);
  32. }
  33. @Override
  34. public void remove(Component component) {
  35. child.remove(component);
  36. }
  37. }
  38. public class Leaf extends Component {
  39. public Leaf(String name) {
  40. super(name);
  41. }
  42. @Override
  43. void print(int level) {
  44. for (int i = 0; i < level; i++) {
  45. System.out.print("--");
  46. }
  47. System.out.println("left:" + name);
  48. }
  49. @Override
  50. public void add(Component component) {
  51. throw new UnsupportedOperationException(); // 牺牲透明性换取单一职责原则,这样就不用考虑是叶子节点还是组合节点
  52. }
  53. @Override
  54. public void remove(Component component) {
  55. throw new UnsupportedOperationException();
  56. }
  57. }
  58. public class Client {
  59. public static void main(String[] args) {
  60. Composite root = new Composite("root");
  61. Component node1 = new Leaf("1");
  62. Component node2 = new Composite("2");
  63. Component node3 = new Leaf("3");
  64. root.add(node1);
  65. root.add(node2);
  66. root.add(node3);
  67. Component node21 = new Leaf("21");
  68. Component node22 = new Composite("22");
  69. node2.add(node21);
  70. node2.add(node22);
  71. Component node221 = new Leaf("221");
  72. node22.add(node221);
  73. root.print();
  74. }
  75. }
  76. 运行结果
  77. Composite:root
  78. --left:1
  79. --Composite:2
  80. ----left:21
  81. ----Composite:22
  82. ------left:221
  83. --left:3

JDK中的体现

  • javax.swing.JComponent#add(Component)
  • java.awt.Container#add(Component)
  • java.util.Map#putAll(Map)
  • java.util.List#addAll(Collection)
  • java.util.Set#addAll(Collection)