设计模式学习笔记——组合模式

原文:http://blog.youkuaiyun.com/hackerain/article/details/7563915

 定义:

对象组合成树形结构以表示“部分—整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性

组合模式主要用来处理一些具有“容器特征”的对象,即他们在充当对象的同时,又可以作为容器包含其他的多个对象。也就是说组合模式表达的是一种树形的结构,将数据结构中的“树”,用面向对象的方式表现出来了,而且表达的是一种多叉树。

看如下的通用类图:


源代码如下:

 

[java]   view plain copy
  1. /* 
  2.  * 抽象节点,定义所有节点的共性 
  3.  */  
  4. public abstract class Component {  
  5.     private String data;  
  6.   
  7.     public String getData() {  
  8.         return data;  
  9.     }  
  10.   
  11.     public void setData(String data) {  
  12.         this.data = data;  
  13.     }  
  14. }  

 

[java]   view plain copy
  1. /* 
  2.  * 叶节点,相当于单个对象 
  3.  */  
  4. public class Leaf extends Component {  
  5.       
  6. }  

 

[java]   view plain copy
  1. /* 
  2.  * 分支节点,相当于组合对象 
  3.  */  
  4. public class Branch extends Component {  
  5.       
  6.     private ArrayList<Component> components=new ArrayList<Component>();  
  7.       
  8.     public void add(Component component){  
  9.         this.components.add(component);  
  10.     }  
  11.       
  12.     public void remove(Component component){  
  13.         this.components.remove(component);  
  14.     }  
  15.       
  16.     public ArrayList<Component> getChildren(){  
  17.         return this.components;  
  18.     }  
  19. }  

 

[java]   view plain copy
  1. public class Client {  
  2.     public static void main(String[] args) {  
  3.           
  4.         //定义根节点  
  5.         Branch root=new Branch();  
  6.         root.setData("root");  
  7.           
  8.         //分支1  
  9.         Branch branch1=new Branch();  
  10.         branch1.setData("branch1");  
  11.           
  12.         //分支2  
  13.         Branch branch2=new Branch();  
  14.         branch2.setData("branch2");  
  15.           
  16.         //叶节点1  
  17.         Leaf leaf1=new Leaf();  
  18.         leaf1.setData("leaf1");  
  19.           
  20.         //叶节点2  
  21.         Leaf leaf2=new Leaf();  
  22.         leaf2.setData("leaf2");  
  23.                   
  24.         //叶节点3  
  25.         Leaf leaf3=new Leaf();  
  26.         leaf3.setData("leaf3");  
  27.                   
  28.         //叶节点4  
  29.         Leaf leaf4=new Leaf();  
  30.         leaf4.setData("leaf4");  
  31.           
  32.         root.add(branch1);  
  33.         root.add(branch2);  
  34.         root.add(leaf1);  
  35.           
  36.         branch1.add(leaf2);  
  37.           
  38.         branch2.add(leaf3);  
  39.         branch2.add(leaf4);  
  40.           
  41.         display(root);  
  42.     }  
  43.       
  44.     //遍历树,输出数据  
  45.     public static void display(Branch root){  
  46.         for(Component b : root.getChildren()){  
  47.             if(b instanceof Leaf){  
  48.                 System.out.println(b.getData());  
  49.             }  
  50.             else{  
  51.                 System.out.println(b.getData());  
  52.                 display((Branch)b);  
  53.             }     
  54.         }  
  55.     }  
  56. }  


组合模式的优点:

1、一个树形结构中所有节点都是Component,局部和整体对调用者来说没有区别,高层模块不必关心自己处理的是单个对象还是整个组合结构,简化了高层模块的代码。

2、节点增加自由


后序:

对这个模式现在还不是很理解,这不就是树结构的面向对象的表示吗?还有其他的什么深意吗?有待于通过实践加深理解。

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值