zoukankan      html  css  js  c++  java
  • Composite (组合模式)

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

    角色:

    1) 抽象构建角色 (Component)

    2) 叶子节点角色 (Leaf)

    3) 组合构建角色 (Composite)

    4) 客户端 (Client)

    Component.java

    [java] view plain copy
    1. package com.composite ;  
    2.   
    3. import java.util.List ;  
    4.   
    5. //抽象构件角色  
    6. public interface Component  
    7.   
    8. {  
    9.       public void add (Component component) ;  
    10.   
    11.       public void remove (Component component) ;  
    12.   
    13.       public void doSomeThing () ;  
    14.   
    15.       public List<Component> getAll () ;  
    16.   
    17. }  

    Leaf.java

    [java] view plain copy
    1. package com.composite ;   
    2.   
    3. import java.util.List ;  
    4.   
    5. //叶子角色  
    6. public class Leaf implements Component  
    7.   
    8. {  
    9.     public void add (Component component)  
    10.     {  
    11.   
    12.     }  
    13.     public void remove (Component component)  
    14.     {  
    15.   
    16.     }  
    17.        
    18.     public List<Component> getAll ()   
    19.     {  
    20.         return null ;  
    21.     }  
    22.   
    23.     public void doSomeThing ()   
    24.     {  
    25.   
    26.         System.out.println("hello") ;  
    27.   
    28.     }  
    29.   
    30. }  

    Composite.java

    [java] view plain copy
    1. package com.composite ;  
    2.   
    3. import java.util.List ;  
    4. import java.util.ArrayList ;  
    5.   
    6. //组合角色  
    7. public class Composite implements Component  
    8.   
    9. {  
    10.     private List<Component> list = new ArrayList<Component> () ;  
    11.   
    12.     public void add (Component component)  
    13.     {  
    14.          list.add(component) ;  
    15.     }  
    16.   
    17.     public void remove (Component component)  
    18.     {  
    19.   
    20.          list.remove(component) ;  
    21.   
    22.     }  
    23.   
    24.     public List<Component> getAll ()   
    25.     {  
    26.           return this.list ;  
    27.   
    28.     }  
    29.   
    30.     //隐式递归  如果 遍历后发现component是叶子节点,则打印;反之,隐式递归  
    31.   
    32.     public void doSomeThing ()   
    33.     {  
    34.         for (Component component : list)  
    35.         {  
    36.             component.doSomeThing() ;     
    37.   
    38.         }  
    39.   
    40.      }  
    41.    
    42.   
    43. }  

    Client.java

    [java] view plain copy
    1. package com.composite ;  
    2.   
    3. //客户端角色  
    4. public class Client  
    5. {  
    6.     public static void main(String[] args)  
    7.     {  
    8.         //创建两个叶子构件  
    9.   
    10.         Component leaf1 = new Leaf () ;  
    11.   
    12.         Component leaf2 = new Leaf () ;  
    13.   
    14.         //创建一个组合构件  
    15.         Component com1 = new Composite () ;  
    16.   
    17.         com1.add (leaf1) ;  
    18.   
    19.         com1.add (leaf2) ;  
    20.           
    21.         //创建两个叶子构件  
    22.         Component leaf3 = new Leaf () ;  
    23.   
    24.         Component leaf4 = new Leaf () ;  
    25.           
    26.         //创建一个组合构件  
    27.         Component com2 = new Composite () ;  
    28.           
    29.         //把第一个组件装进第二个组件里  
    30.         com2.add (com1) ;  
    31.   
    32.         com2.add (leaf3) ;  
    33.   
    34.         com2.add (leaf4) ;             
    35.           
    36.         com2.doSomeThing () ;  
    37.           
    38.         //com2.remove(com1) ;  
    39.         //com2.remove(leaf2) ;//在这移除不掉leaf2,只能com1.remove(leaf2)  
    40.         //System.out.println(com2.getAll()) ;  
    41.   
    42.         }  
    43.   
    44.   
    45. }  

    总结:

    优点:

    组合模式(Composite)可以让客户端的代码变的简单,不会那么繁琐,一致性的处理单个对象和组合对象

    使用组合模式可以很好的增加新的叶子构件

    缺点:

    控制树枝节点构件不容易

  • 相关阅读:
    Android调用Camera API 拍照导致图片变形
    [转]Android PorterDuff.Mode效果
    Android视频录制
    Android调用Camera API 拍照
    Android调用系统拍照
    android:windowIsTranslucent影响Activity生命周期onStop
    ProgressBar自定义之后图片拉伸的解决办法
    android开源ORM框架OrmLite使用教程
    AutoCompleteTextView源码分析
    Android App安全加固
  • 原文地址:https://www.cnblogs.com/hoobey/p/5294389.html
Copyright © 2011-2022 走看看