【设计模式-07】Composite组合模式

简要说明

一、代码实现

  • 定义抽象节点类 Node ,定义抽象方法 public abstract void print();
  • 定义叶子节点类 LeafNode ,继承Node节点,实现 print()抽象方法,叶子节点没有子节点
  • 定义子节点类BranchNode,继承Node节点,实现 print()抽象方法,子节点既可以有子节点,也又可以有叶子节点
  • 定义一个树状目录结构,使用递归打印树状目录结构
import java.util.ArrayList;
import java.util.List;

/**
* @description: composite组合模式
* @author: flygo
* @time: 2022/7/20 14:05
*/
public class CompositeMain {
    
    public static void main(String[] args) {
        BranchNode root = new BranchNode("root");
        
        BranchNode chapter1 = new BranchNode("chapter1");
        BranchNode chapter2 = new BranchNode("chapter2");
        
        Node r1 = new LeafNode("r1");
        Node c11 = new LeafNode("c11");
        Node c12 = new LeafNode("c12");
        
        BranchNode b21 = new BranchNode("section21");
        Node c211 = new LeafNode("c211");
        Node c212 = new LeafNode("c212");
        
        root.add(chapter1).add(chapter2).add(r1);
        chapter1.add(c11).add(c12);
        chapter2.add(b21);
        b21.add(c211).add(c212);
        
        tree(root, 0);
    }
    
    private static void tree(Node node, int depth) {
        for (int i = 0; i < depth; i++) {
            System.out.print("--");
        }
        node.print();
        
        if (node instanceof BranchNode) {
            for (Node n : ((BranchNode) node).nodes) {
                tree(n, depth + 1);
            }
        }
    }
}

abstract class Node {
    public abstract void print();
}

/**
* @description: 叶子节点-不能有子节点
* @author: flygo
* @time: 2022/7/20 14:10
*/
class LeafNode extends Node {
    String content;
    
    public LeafNode(String content) {
        this.content = content;
    }
    
    @Override
    public void print() {
        System.out.println(content);
    }
}

/**
* @description: 子节点-可以有子节点和叶子节点
* @author: flygo
* @time: 2022/7/20 14:10
*/
class BranchNode extends Node {
    
    // 子节点可以有子节点和叶子节点
    List<Node> nodes = new ArrayList<>();
    
    String name;
    
    public BranchNode(String name) {
        this.name = name;
    }
    
    @Override
    public void print() {
        System.out.println(name);
    }
    
    public BranchNode add(Node node) {
        this.nodes.add(node);
        return this;
    }
}

二、源码地址

https://github.com/jxaufang168/Design-Patternsicon-default.png?t=N7T8https://github.com/jxaufang168/Design-Patterns

相关推荐

  1. 设计模式——组合模式Composite

    2024-01-18 07:12:03       37 阅读
  2. 设计模式--组合模式Composite Pattern)

    2024-01-18 07:12:03       50 阅读
  3. 设计模式组合模式Composite Pattern)

    2024-01-18 07:12:03       35 阅读
  4. 设计模式】10、composite 组合模式

    2024-01-18 07:12:03       35 阅读
  5. 设计模式-组合模式Composite(结构型)

    2024-01-18 07:12:03       29 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-18 07:12:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-18 07:12:03       101 阅读
  3. 在Django里面运行非项目文件

    2024-01-18 07:12:03       82 阅读
  4. Python语言-面向对象

    2024-01-18 07:12:03       91 阅读

热门阅读

  1. React父组件向子组件传值

    2024-01-18 07:12:03       59 阅读
  2. 笨蛋学设计模式结构型模式-组合模式【12】

    2024-01-18 07:12:03       45 阅读
  3. MATLAB Fundamentals>>Representing Discrete Categories

    2024-01-18 07:12:03       62 阅读
  4. Hadoop之mapreduce参数大全-7

    2024-01-18 07:12:03       45 阅读
  5. flutter 播放SVGA动图

    2024-01-18 07:12:03       63 阅读
  6. Spring Boot整合Junit

    2024-01-18 07:12:03       46 阅读
  7. esp32-c-简单应用笔记

    2024-01-18 07:12:03       45 阅读
  8. 消息队列之RabbitMQ工作模式

    2024-01-18 07:12:03       48 阅读
  9. Spring Boot整合Junit,@RunWith和@SpringBootTest的使用

    2024-01-18 07:12:03       50 阅读
  10. LUA 对象转excel

    2024-01-18 07:12:03       41 阅读
  11. Bitcoin的Covenants——合同化管理UTXO的花费方式

    2024-01-18 07:12:03       74 阅读
  12. 在 Centos 7.9 中,安装与配置 Docker 20.10.18

    2024-01-18 07:12:03       54 阅读
  13. flask不使用flask-login插件

    2024-01-18 07:12:03       57 阅读