典型例子: 管道过滤器. 下面是一个例子,不贴合实际,但是用到了 合成模式. 1 public interface SalaryComputer { 2 public int computer(int m, int n); 3 } 1 public class Add implements SalaryComputer { 2 @Override 3 public int computer(int m, int n) { 4 System.out.println(m + "+" + n
模式的定义 组合模式(Composite Pattern)定义如下: Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. 将对象组合成树形结构以表示"部分-整体"的层次结构,使得用户对单个对象和组合对象的使用具有一致性.