• 设计模式之组合模式


    一.组合模式的定义

    组合模式将对象组合成树状结构以表示“整体-部分”的层次关系;

    组合模式使得用户对单个对象和组合对象的访问具有一致性。

    二.组合模式的角色及职责

    1.Component

    这是组合中的对象声明接口,在适当的情况下,实现所有类共有接口的默认行为。声明一个接口用于访问和管理Component子部件。

    2.Leaf

    在组合中表示叶子节点对象,叶子节点没有子节点。

    3.Composite

    定义有枝节点行为,用来存储子部件,在Component接口中实现与子部件有关的操作,如增加(add)和删除(remove)等。

    组合模式的UML图:

    实例代码如下:

     1 public abstract class Component {
     2     private String name;
     3 
     4     public void setName(String name) {
     5         this.name = name;
     6     }
     7 
     8     public String getName() {
     9         return name;
    10     }
    11 
    12     protected abstract void add(Component c);
    13 
    14     protected abstract void remove(Component c);
    15 
    16     protected abstract void display(int depth);
    17 }
     1 public class Leaf extends Component {
     2 
     3     public Leaf(String name){
     4         setName(name);
     5     }
     6 
     7     @Override
     8     protected void add(Component c) {
     9         //叶子节点不能添加子节点,因此不做实现
    10     }
    11 
    12     @Override
    13     protected void remove(Component c) {
    14         //叶子节点不能添加子节点,因此不做实现
    15     }
    16 
    17     @Override
    18     protected void display(int depth) {
    19         String temp = "";
    20         for (int i=0; i<depth; i++){
    21             temp += "-";
    22         }
    23         System.out.println(temp + getName());
    24     }
    25 }
     1 public class Composite extends Component {
     2     private List<Component> children = new ArrayList<>();
     3 
     4     public Composite(String name){
     5         setName(name);
     6     }
     7 
     8     @Override
     9     protected void add(Component c) {
    10         children.add(c);
    11     }
    12 
    13     @Override
    14     protected void remove(Component c) {
    15         children.remove(c);
    16     }
    17 
    18     @Override
    19     protected void display(int depth) {
    20         String temp = "";
    21         for (int i=0; i<depth; i++){
    22             temp += "-";
    23         }
    24         System.out.println(temp + getName());
    25 
    26         for (Component child : children){
    27             child.display(depth + 2);
    28         }
    29     }
    30 }

    以下是测试类:

     1 public class CompositeTest {
     2 
     3     public static void main(String[] args) {
     4         Component root = new Composite("根节点");
     5 
     6         Component leftChild = new Composite("左节点");
     7         Component rightChild = new Composite("右节点");
     8 
     9         Component leftChild_1 = new Leaf("左节点-叶子1");
    10         Component leftChild_2 = new Leaf("左节点-叶子2");
    11 
    12         Component rightChild_leaf = new Leaf("右节点—叶子");
    13 
    14         leftChild.add(leftChild_1);
    15         leftChild.add(leftChild_2);
    16 
    17         rightChild.add(rightChild_leaf);
    18 
    19         root.add(leftChild);
    20         root.add(rightChild);
    21 
    22         root.display(1);
    23     }
    24 
    25 }

    运行结果如下:

    三.组合模式的使用场景

    1.想要表示对象的整体-部分的层次结构。

    2.希望用户忽略组合对象与单个对象的不同,用户将统一的使用组合结构中的所有对象。

  • 相关阅读:
    socket.io+angular.js+express.js做个聊天应用(二)
    [原创]Python通过Thrift连接HBase
    [原创]安装Sqoop并验证
    使用PostgreSQL、Hibernate 构建 NoSQL
    [原创]HBase客户端开发举例(第三部分)
    [原创]HBase客户端开发举例(第二部…
    [原创]全分布模式下Hadoop安装
    Samba的基本配置
    常见设计模式举例 转载有改动
    【转载】hibernate中使用ehcache
  • 原文地址:https://www.cnblogs.com/lixiuyu/p/5947120.html
Copyright © 2020-2023  润新知