From 63402cc583e6e06302d1d631d2b36847e1e91e39 Mon Sep 17 00:00:00 2001 From: "robin(lc)" Date: Mon, 9 May 2022 18:18:52 +0800 Subject: [PATCH] =?UTF-8?q?:sparkles:=20=E7=BB=84=E5=90=88=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../structure/composite/Component.java | 24 ++++++++++++++ .../structure/composite/Composite.java | 26 +++++++++++++++ .../structure/composite/LazyComponent.java | 17 ++++++++++ .../structure/composite/Main.java | 32 +++++++++++++++++++ .../structure/composite/PackageInfo.java | 10 ++++++ .../structure/composite/WorkComponent.java | 17 ++++++++++ 6 files changed, 126 insertions(+) create mode 100644 datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/Component.java create mode 100644 datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/Composite.java create mode 100644 datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/LazyComponent.java create mode 100644 datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/Main.java create mode 100644 datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/PackageInfo.java create mode 100644 datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/WorkComponent.java diff --git a/datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/Component.java b/datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/Component.java new file mode 100644 index 0000000..d72af16 --- /dev/null +++ b/datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/Component.java @@ -0,0 +1,24 @@ +package org.alis.smallcc.designpatterns.structure.composite; + +/** + * 组件 + * + * @author robin + * @date 2022/5/9 17:52 + */ +public interface Component { + + /** + * 炒作 + */ + void operation(); + + + /** + * 组合器 + * + * @param component 组件 + */ + void addComponent(Component component); + +} diff --git a/datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/Composite.java b/datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/Composite.java new file mode 100644 index 0000000..2e486c6 --- /dev/null +++ b/datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/Composite.java @@ -0,0 +1,26 @@ +package org.alis.smallcc.designpatterns.structure.composite; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author lc + * @date 2022/5/9 17:51 + */ +public class Composite implements Component { + + private final List COMPONENT_LIST = new ArrayList<>(); + + @Override + public void operation() { + System.out.println("\n" + hashCode() + " 组合器执行:\n"); + for (Component component : COMPONENT_LIST) { + component.operation(); + } + } + + @Override + public void addComponent(Component component) { + COMPONENT_LIST.add(component); + } +} diff --git a/datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/LazyComponent.java b/datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/LazyComponent.java new file mode 100644 index 0000000..c7b6cc3 --- /dev/null +++ b/datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/LazyComponent.java @@ -0,0 +1,17 @@ +package org.alis.smallcc.designpatterns.structure.composite; + +/** + * @author lc + * @date 2022/5/9 17:57 + */ +public class LazyComponent implements Component { + @Override + public void operation() { + System.out.println(hashCode() + "懒惰节点执行"); + } + + @Override + public void addComponent(Component component) { + throw new UnsupportedOperationException(); + } +} diff --git a/datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/Main.java b/datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/Main.java new file mode 100644 index 0000000..ce1880d --- /dev/null +++ b/datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/Main.java @@ -0,0 +1,32 @@ +package org.alis.smallcc.designpatterns.structure.composite; + +import java.util.concurrent.ThreadLocalRandom; + +/** + * @author lc + * @date 2022/5/9 18:00 + */ +public class Main { + public static void main(String[] args) { + // 创建一个根 + Component root = new Composite(); + + for (int i = 0; i < 3; i++) { + Composite composite = new Composite(); + // 创建10个懒惰或者勤快节点 + for (int j = 0; j < 10; j++) { + int i1 = ThreadLocalRandom.current().nextInt(0, 10); + Component component; + if (i1 % 2 == 0) { + component = new WorkComponent(); + } else { + component = new LazyComponent(); + } + composite.addComponent(component); + } + root.addComponent(composite); + } + + root.operation(); + } +} diff --git a/datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/PackageInfo.java b/datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/PackageInfo.java new file mode 100644 index 0000000..bfd10ce --- /dev/null +++ b/datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/PackageInfo.java @@ -0,0 +1,10 @@ +package org.alis.smallcc.designpatterns.structure.composite; + +/** + * 组合模式 + * + * @author lc + * @date 2022/5/9 17:51 + */ +public class PackageInfo { +} diff --git a/datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/WorkComponent.java b/datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/WorkComponent.java new file mode 100644 index 0000000..3f5eda4 --- /dev/null +++ b/datastructure/src/main/java/org/alis/smallcc/designpatterns/structure/composite/WorkComponent.java @@ -0,0 +1,17 @@ +package org.alis.smallcc.designpatterns.structure.composite; + +/** + * @author lc + * @date 2022/5/9 17:57 + */ +public class WorkComponent implements Component { + @Override + public void operation() { + System.out.println(hashCode() + "工作节点执行"); + } + + @Override + public void addComponent(Component component) { + throw new UnsupportedOperationException(); + } +}