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

     需求中是体现部分与整体层次的结构时,以及你希望用户忽略组合对象与单个对象的不同,统一的使用组合结构中的所有对象时,

    就应该考虑组合模式了

    代码如下:

     1 public abstract class Component {
     2     protected String name;
     3 
     4     public Component(String name) {
     5         super();
     6         this.name = name;
     7     }
     8     
     9     /**通常都用add和remove方法来提供增加或移除树叶或树枝的功能*/
    10     public abstract void add(Component com);
    11     public abstract void remove(Component com);
    12     public abstract void display(int depth);
    13     
    14 }
    Component
     1 public class Leaf extends Component{
     2 
     3     public Leaf(String name) {
     4         super(name);
     5     }
     6 
     7     /**
     8      * 由于叶子没有增加分枝和树叶,所以add和remove方法实现没有意义,
     9      * 但是这样可以消除叶节点,和枝节点在抽象层次的区别,它们具有完全一致的接口
    10      */
    11     @Override
    12     public void add(Component com) {
    13         System.out.println("Leaf no add");
    14     }
    15 
    16     @Override
    17     public void remove(Component com) {
    18         System.out.println("Leaf no remove");
    19     }
    20 
    21     @Override
    22     public void display(int depth) {
    23         System.out.println(String.valueOf(depth)+"----name----"+name);
    24     }
    25 
    26 }
    Leaf
     1 import java.util.ArrayList;
     2 import java.util.List;
     3 
     4 public class Composite extends Component {
     5     private List<Component> children = new ArrayList<Component>();
     6     public Composite(String name) {
     7         super(name);
     8     }
     9 
    10     @Override
    11     public void add(Component com) {
    12         children.add(com);
    13         
    14     }
    15 
    16     @Override
    17     public void remove(Component com) {
    18         children.remove(com);
    19         
    20     }
    21 
    22     @Override
    23     public void display(int depth) {
    24         System.out.println(String.valueOf(depth)+"----name----"+name);
    25         for (Component comp : children) {
    26             comp.display(depth+2);
    27         }
    28         
    29     }
    30 
    31 }
    Composite
     1 public class CompositeTest {
     2 
     3     public static void main(String[] args) {
     4         //生成树根 root 长出叶子:Leaf A,Leaf B
     5         Composite root = new Composite("root");
     6         root.add(new Leaf("Leaf A"));
     7         root.add(new Leaf("Leaf B"));
     8         
     9         //长出分枝Composite X ,Composite X上长出叶子:Leaf XA,Leaf XB
    10         Composite comp = new Composite("Composite X");
    11         comp.add(new Leaf("Leaf XA"));
    12         comp.add(new Leaf("Leaf XB"));
    13         root.add(comp);
    14         
    15         //长出分枝Composite Y ,Composite Y上长出叶子:Leaf YA,Leaf YB
    16         Composite comp2 = new Composite("Composite Y");
    17         comp2.add(new Leaf("Leaf YA"));
    18         comp2.add(new Leaf("Leaf YB"));
    19         root.add(comp2);
    20         
    21         root.add(new Leaf("Leaf C"));
    22         Leaf leaf = new Leaf("Leaf D");
    23         root.add(leaf);
    24         root.remove(leaf);
    25         
    26         root.display(1);
    27     }
    28 
    29 }
    test

    结果:

    1----name----root
    3----name----Leaf A
    3----name----Leaf B
    3----name----Composite X
    5----name----Leaf XA
    5----name----Leaf XB
    3----name----Composite Y
    5----name----Leaf YA
    5----name----Leaf YB
    3----name----Leaf C
    结果
  • 相关阅读:
    IDEA右键新建时没有Java Class选项
    捕获摄像头视频VC
    重叠IO与IOCP
    (八)内存管理与内存分配
    DebugView使用详解
    (六) 中断机制
    (五) proc文件系统
    bash 之备份文件
    bash 遍历目录文件
    (四) linux内核模块编程
  • 原文地址:https://www.cnblogs.com/cai170221/p/13372703.html
Copyright © 2011-2022 走看看