From 9e1e86d93a8cbec977ce41a6a5bcbb8f4a239e88 Mon Sep 17 00:00:00 2001 From: bingor Date: Fri, 14 Oct 2022 19:25:40 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9D=A6=E5=85=8B=E5=A4=A7=E6=88=98=EF=BC=88?= =?UTF-8?q?=E4=B8=80=E6=9C=9F=EF=BC=89-=E8=AE=BE=E8=AE=A1=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F-=E7=BB=84=E5=90=88composite=EF=BC=88=E6=A0=91?= =?UTF-8?q?=E7=8A=B6=E7=BB=93=E6=9E=84=E4=B8=93=E7=94=A8=E6=A8=A1=E5=BC=8F?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/msb/composite/BranchNode.java | 40 +++++++++++++++ src/main/java/com/msb/composite/LeafNode.java | 27 ++++++++++ src/main/java/com/msb/composite/Main.java | 50 +++++++++++++++++++ src/main/java/com/msb/composite/Node.java | 17 +++++++ 4 files changed, 134 insertions(+) create mode 100644 src/main/java/com/msb/composite/BranchNode.java create mode 100644 src/main/java/com/msb/composite/LeafNode.java create mode 100644 src/main/java/com/msb/composite/Main.java create mode 100644 src/main/java/com/msb/composite/Node.java 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