1. 意图(Intent)

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

2. 类图(Class Diagram)

组件(Component)类是组合类(Composite)和叶子类(Leaf)的父类,可以把组合类看成是树的中间节点。

组合对象拥有一个或者多个组件对象,因此组合对象的操作可以委托给组件对象去处理,而组件对象可以是另一个组合对象或者叶子对象。
2b8bfd57-b4d1-4a75-bfb0-bcf1fba4014a.png

3. 实现(Implementation)

I 雇员案例1

直接看一个例子吧,每个员工都有姓名、部门、薪水这些属性,同时还有下属员工集合(虽然可能集合为空),而下属员工和自己的结构是一样的,也有姓名、部门这些属性,同时也有他们的下属员工集合。

  1. public class Employee {
  2. private String name;
  3. private String dept;
  4. private int salary;
  5. private List<Employee> subordinates; // 下属
  6. public Employee(String name,String dept, int sal) {
  7. this.name = name;
  8. this.dept = dept;
  9. this.salary = sal;
  10. subordinates = new ArrayList<Employee>();
  11. }
  12. public void add(Employee e) {
  13. subordinates.add(e);
  14. }
  15. public void remove(Employee e) {
  16. subordinates.remove(e);
  17. }
  18. public List<Employee> getSubordinates(){
  19. return subordinates;
  20. }
  21. public String toString(){
  22. return ("Employee :[ Name : " + name + ", dept : " + dept + ", salary :" + salary+" ]");
  23. }
  24. }

通常,这种类需要定义 add(node)、remove(node)、getChildren() 这些方法。

II 雇员案例2

组件(Component)类

  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. }

组合类(Composite)

  1. public class Composite extends Component {
  2. private List<Component> child;
  3. public Composite(String name) {
  4. super(name);
  5. child = new ArrayList<>();
  6. }
  7. @Override
  8. void print(int level) {
  9. for (int i = 0; i < level; i++) {
  10. System.out.print("--");
  11. }
  12. System.out.println("Composite:" + name);
  13. for (Component component : child) {
  14. component.print(level + 1);
  15. }
  16. }
  17. @Override
  18. public void add(Component component) {
  19. child.add(component);
  20. }
  21. @Override
  22. public void remove(Component component) {
  23. child.remove(component);
  24. }
  25. }

叶子类(Leaf)

  1. public class Leaf extends Component {
  2. public Leaf(String name) {
  3. super(name);
  4. }
  5. @Override
  6. void print(int level) {
  7. for (int i = 0; i < level; i++) {
  8. System.out.print("--");
  9. }
  10. System.out.println("left:" + name);
  11. }
  12. @Override
  13. public void add(Component component) {
  14. // 牺牲透明性换取单一职责原则,这样就不用考虑是叶子节点还是组合节点
  15. throw new UnsupportedOperationException();
  16. }
  17. @Override
  18. public void remove(Component component) {
  19. throw new UnsupportedOperationException();
  20. }
  21. }

客户端代码

  1. public class Client {
  2. public static void main(String[] args) {
  3. Composite root = new Composite("root");
  4. Component node1 = new Leaf("1");
  5. Component node2 = new Composite("2");
  6. Component node3 = new Leaf("3");
  7. root.add(node1);
  8. root.add(node2);
  9. root.add(node3);
  10. Component node21 = new Leaf("21");
  11. Component node22 = new Composite("22");
  12. node2.add(node21);
  13. node2.add(node22);
  14. Component node221 = new Leaf("221");
  15. node22.add(node221);
  16. root.print();
  17. }
  18. }
  1. Composite:root
  2. --left:1
  3. --Composite:2
  4. ----left:21
  5. ----Composite:22
  6. ------left:221
  7. --left:3


4. 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)