diff --git a/src/main/java/com/msb/composite/BranchNode.java b/src/main/java/com/msb/composite/BranchNode.java new file mode 100644 index 0000000..65127cd --- /dev/null +++ b/src/main/java/com/msb/composite/BranchNode.java @@ -0,0 +1,40 @@ +package com.msb.composite;/** + * @Author bingor + * @Date 2022/10/14 18:49 + * @Description: com.msb.composite + * @Version: 1.0 + */ + +import java.util.ArrayList; +import java.util.List; + +/** + *@ClassName BranchNode + *@Description TODO + *@Author bingor + *@Date 2022/10/14 18:49 + *@Version 3.0 + */ +public class BranchNode extends Node { + + private String name; + private List nodes = new ArrayList<>(); + + public BranchNode(String name) { + this.name = name; + } + + @Override + public void print() { + System.out.println(this.name); + } + + public void addNode(Node node) { + nodes.add(node); + } + + public List getNodes() { + return this.nodes; + } + +} diff --git a/src/main/java/com/msb/composite/LeafNode.java b/src/main/java/com/msb/composite/LeafNode.java new file mode 100644 index 0000000..727e51e --- /dev/null +++ b/src/main/java/com/msb/composite/LeafNode.java @@ -0,0 +1,27 @@ +package com.msb.composite;/** + * @Author bingor + * @Date 2022/10/14 16:22 + * @Description: com.msb.composite + * @Version: 1.0 + */ + +/** + *@ClassName LeafNode + *@Description TODO + *@Author bingor + *@Date 2022/10/14 16:22 + *@Version 3.0 + */ +public class LeafNode extends Node { + + private String content; + + public LeafNode(String content) { + this.content = content; + } + + @Override + public void print() { + System.out.println(this.content); + } +} diff --git a/src/main/java/com/msb/composite/Main.java b/src/main/java/com/msb/composite/Main.java new file mode 100644 index 0000000..68a0675 --- /dev/null +++ b/src/main/java/com/msb/composite/Main.java @@ -0,0 +1,50 @@ +package com.msb.composite;/** + * @Author bingor + * @Date 2022/10/14 18:54 + * @Description: com.msb.composite + * @Version: 1.0 + */ + +/** + *@ClassName Main + *@Description TODO + *@Author bingor + *@Date 2022/10/14 18:54 + *@Version 3.0 + */ +public class Main { + + public static void main(String[] args) { + BranchNode root = new BranchNode("root"); + BranchNode chapter1 = new BranchNode("chapter1"); + BranchNode chapter2 = new BranchNode("chapter2"); + LeafNode c11 = new LeafNode("c11"); + LeafNode c12 = new LeafNode("c12"); + BranchNode c2b1 = new BranchNode("c2b1"); + LeafNode c2b11 = new LeafNode("c2b11"); + LeafNode c2b12 = new LeafNode("c2b12"); + + root.addNode(chapter1); + root.addNode(chapter2); + chapter1.addNode(c11); + chapter1.addNode(c12); + chapter2.addNode(c2b1); + c2b1.addNode(c2b11); + c2b1.addNode(c2b12); + + tree(root, 0); + } + + private static void tree(Node root, int depth) { + for (int i=0; i