组合模式

来源:互联网 发布:mac的onedrive在哪里 编辑:程序博客网 时间:2024/05/17 01:56

组合模式

定义:允许你将对象组合成树形来表达结构来表现“整体/部分”层次结构。组合能让用户以一致的方式处理个别对象及对象组合。

角色

  1. component :是组合中对象的接口,适当情况下,实现所有类共有方法的默认行为,声明一个接口,用于管理和访问component子部件
  2. composite:定义具有叶节点的组件的行为
  3. leaf:定义叶节点的行为
  4. client:使用component接口操作组件行为

适用场合

  1. 想表示对象的部分-整体层次结构
  2. 希望用户忽略组合对象与单个对象的不同,使用户一同一种方式使用组合结构中的所有对象。

好处

  1. 定义了包含基本对象和组合对象的类层次结构,基本对象可以被组合成更复杂的组合对象,而这个组合对象有可以被组合。
  2. 简化客户代码,客户可以一直地使用组合结构和单个对象,通常用户不知道处理的是一个叶节点还是一个组合组件。
  3. 使得更容易增加新类型的组件, 新定义的Composite或leaf子类自动地与已有的结构和客户代码一起工作,客户程序不需要因为新的Component类而改变。
  4. 使你的设计变得更一般化。
代码示例

[java] view plaincopy
  1. /** 
  2.  * 组件的接口 
  3.  * @author sky 
  4.  * 
  5.  */  
  6. public abstract class FileComponent {  
  7.       
  8.     public void add(FileComponent component){  
  9.         throw new UnsupportedOperationException();  
  10.     }  
  11.       
  12.     public void remove(FileComponent component){  
  13.         throw new UnsupportedOperationException();  
  14.     }  
  15.       
  16.     public FileComponent getChild(int index){  
  17.         throw new UnsupportedOperationException();  
  18.     }  
  19.       
  20.     public String getName(){  
  21.         throw new UnsupportedOperationException();  
  22.     }  
  23.       
  24.     public String getDescription(){  
  25.         throw new UnsupportedOperationException();  
  26.     }  
  27.   
  28. }  
  29.   
  30. /** 
  31.  * 组合件的组件,可以包含组件或者叶节点 
  32.  * @author sky 
  33.  * 
  34.  */  
  35. public class FileComposite extends FileComponent {  
  36.       
  37.     private String name;  
  38.       
  39.     private List<FileComponent> list;  
  40.       
  41.     public FileComposite(String name){  
  42.         this.name = name;  
  43.         list = new ArrayList<>();  
  44.     }  
  45.   
  46.     @Override  
  47.     public void add(FileComponent component) {  
  48.         list.add(component);  
  49.     }  
  50.   
  51.     @Override  
  52.     public void remove(FileComponent component) {  
  53.         list.remove(component);  
  54.     }  
  55.   
  56.     @Override  
  57.     public FileComponent getChild(int index) {  
  58.         if ( index>=0&&list.size()>index ) {  
  59.             return list.get(index);  
  60.         }  
  61.         return null;  
  62.     }  
  63.   
  64.     @Override  
  65.     public String getName() {  
  66.         return name;  
  67.     }  
  68.   
  69.     @Override  
  70.     public String getDescription() {  
  71.         Iterator<FileComponent> iterator = list.iterator();  
  72.         StringBuffer sb = new StringBuffer();  
  73.         sb.append(name).append(":【");  
  74.         while (iterator.hasNext()) {  
  75.             sb.append(iterator.next().getDescription());  
  76.         }  
  77.         sb.append("】");  
  78.         return sb.toString();  
  79.     }  
  80.   
  81. }  
  82.   
  83. /** 
  84.  * leaf叶节点 
  85.  * @author sky 
  86.  * 
  87.  */  
  88. public class FileLeaf extends FileComponent {  
  89.       
  90.     private String name;  
  91.       
  92.     public FileLeaf(String name){  
  93.         this.name = name;  
  94.     }  
  95.   
  96.     @Override  
  97.     public String getName() {  
  98.         return name;  
  99.     }  
  100.   
  101.     @Override  
  102.     public String getDescription() {  
  103.         return "  name:"+name;  
  104.     }  
  105.   
  106. }  
  107.   
  108. /** 
  109.  * 测试代码 
  110.  * @author sky 
  111.  * 
  112.  */  
  113. public class Test {  
  114.   
  115.     public static void main(String[] args) {  
  116.         FileComponent directory = new FileComposite("目录1");  
  117.         FileComponent directory2 = new FileComposite("目录2");  
  118.         FileComponent file = new FileLeaf("book");  
  119.         directory2.add(file);  
  120.         file = new FileLeaf("mp3");  
  121.         directory2.add(file);  
  122.         directory.add(directory2);  
  123.         System.out.println(directory.getDescription());  
  124.     }  
  125.   
  126. }  
  127. 输出:  
  128.     目录1:【目录2:【  name:book  name:mp3】】  

0 0