zoukankan      html  css  js  c++  java
  • Composite

    组合模式是整体与部分的关系,一个典型的应用就是树型结构,组合模式可以抽象出三种角色,分别为抽象构建角色(Component)、树枝构建角色(Composite)、树叶构建角色(Leaf)。

    抽象构建角色:这是一个抽象的角色,它给参加组合的对象规定了统一的接口,给出了公有的接口和行为。

    树枝构建角色:代表参加组合的有子类的对象,并给出树枝构建对象的行为。

    树叶构建角色:代表参加组合的树叶对象,即没有子节点的对象。

    适用性
    以下情况下适用Composite模式:
      1.你想表示对象的部分-整体层次结构
      2.你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。

     1 //Component为组合的对象声明公共接口
     2 public abstract class Component {
     3 
     4     protected String flag;
     5     
     6     public Component(String flag){
     7         this.flag = flag;
     8     }
     9     
    10     //通过 Add 和 Remove 来提供增加或移除树叶或树枝的功能
    11     public abstract void add(Component c);
    12     
    13     public abstract void remove(Component c);
    14     
    15     public abstract void display(int depth);
    16     
    17 }
    18 
    19 
    20 class StringUtils{
    21     
    22     public static String getString(int count, String str){
    23         StringBuffer sb = new StringBuffer();
    24         for(int i = 0 ; i < count; i++){
    25             sb.append(str);
    26         }
    27         return sb.toString();
    28     }
    29     
    30 }
     1 //定义枝节点行为,用来存储子部件。
     2 public class Composite extends Component{
     3 
     4     private List<Component> children = new ArrayList<Component>(); 
     5     
     6     public Composite(String flag) {
     7         super(flag);
     8     }
     9 
    10     public void add(Component c) {
    11         if(this == c)return;
    12         children.add(c);
    13     }
    14 
    15     public void remove(Component c) {
    16         children.remove(c);
    17     }
    18 
    19     //显示其枝节点的名字,并对下级进行遍历。
    20     public void display(int depth) {
    21         System.out.println(StringUtils.getString(depth, "-") + " : " + flag);
    22         for(Component c : children){
    23             c.display(depth + 2);
    24         }
    25     }
    26     
    27 }
     1 public class Leaf extends Component{
     2     
     3     public Leaf(String flag) {
     4         super(flag);
     5     }
     6 
     7     //叶子没有再添加分支的功能,所以实现它没有意义。
     8     public void add(Component c) {
     9         System.out.println("can't add a leaf !");
    10     }
    11 
    12     public void remove(Component c) {
    13         System.out.println("can't remove a leaf !");
    14     }
    15 
    16     public void display(int depth) {
    17         System.out.println(StringUtils.getString(depth, "-") + " : " + flag);
    18     }
    19 
    20 }
     1     public static void main(String[] args) {
     2         
     3         Composite root = new Composite("root");
     4         
     5         root.add(new Leaf("Leaf A"));
     6         root.add(new Leaf("Leaf B"));
     7 
     8         Composite comp = new Composite("comp");
     9         comp.add(new Leaf("Leaf C"));
    10         comp.add(new Leaf("Leaf D"));
    11         
    12         root.add(comp);
    13         
    14         Composite comp2 = new Composite("comp2");
    15         comp2.add(new Leaf("Leaf E"));
    16         comp2.add(new Leaf("Leaf F"));
    17         
    18         comp.add(comp2);
    19         
    20         comp.add(new Leaf("Z"));
    21         
    22         root.add(root);
    23         
    24         root.display(1);
    25         
    26     }
    27     

    打印结果:
    - : root
    --- : Leaf A
    --- : Leaf B
    --- : comp
    ----- : Leaf C
    ----- : Leaf D
    ----- : comp2
    ------- : Leaf E
    ------- : Leaf F
    ----- : Z

  • 相关阅读:
    使用Systemctl命令来管理系统服务
    使用lsblk命令列出系统中的块设备
    史上最全 | 1000余个实用尽调网站分类汇编
    ​2021年机器学习什么风向?谷歌大神Quoc Le:把注意力放在MLP上
    上手使用 DeepMind 分布式强化学习框架 Acme ,对开发者超友好
    005-ESP32学习开发(SDK)-新建工程补充-通过官方示例创建工程
    Golang 程序中实现优雅关闭 HTTP SERVER
    Golang的time.NewTicker周期性定时器使用案例
    彻底搞懂golang的GOROOT和GOPATH
    微服务之-ServiceMesh
  • 原文地址:https://www.cnblogs.com/xuekyo/p/2612217.html
Copyright © 2011-2022 走看看