From 9a496e0d258c8d125ee6b62da8bdc69fbe75eab4 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Thu, 3 May 2018 12:13:12 +0400 Subject: [PATCH 1/3] Composite design pattern --- .../composite/CompositeDemo.java | 11 ++++++ .../designpatterns/composite/Department.java | 9 +++++ .../composite/FinancialDepartment.java | 35 +++++++++++++++++++ .../composite/GeneralDepartment.java | 33 +++++++++++++++++ .../composite/SalesDepartment.java | 35 +++++++++++++++++++ 5 files changed, 123 insertions(+) create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/composite/CompositeDemo.java create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/composite/Department.java create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/composite/FinancialDepartment.java create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/composite/GeneralDepartment.java create mode 100644 core-java/src/main/java/com/baeldung/designpatterns/composite/SalesDepartment.java diff --git a/core-java/src/main/java/com/baeldung/designpatterns/composite/CompositeDemo.java b/core-java/src/main/java/com/baeldung/designpatterns/composite/CompositeDemo.java new file mode 100644 index 0000000000..83024b7d21 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/composite/CompositeDemo.java @@ -0,0 +1,11 @@ +package com.baeldung.designpatterns.composite; + +/** + * Created by Gebruiker on 5/3/2018. + */ +public class CompositeDemo { + + public static void main(String args[]) { + Department salesDepartment = new SalesDepartment(); + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/composite/Department.java b/core-java/src/main/java/com/baeldung/designpatterns/composite/Department.java new file mode 100644 index 0000000000..82fa3a3efc --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/composite/Department.java @@ -0,0 +1,9 @@ +package com.baeldung.designpatterns.composite; + +/** + * Created by Gebruiker on 5/1/2018. + */ +public interface Department { + + void printDepartmentName(); +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/composite/FinancialDepartment.java b/core-java/src/main/java/com/baeldung/designpatterns/composite/FinancialDepartment.java new file mode 100644 index 0000000000..dc5a2bb9d9 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/composite/FinancialDepartment.java @@ -0,0 +1,35 @@ +package com.baeldung.designpatterns.composite; + +/** + * Created by Gebruiker on 5/1/2018. + */ +public class FinancialDepartment implements Department { + + private Integer id; + private String name; + + public FinancialDepartment(Integer id, String name) { + this.id = id; + this.name = name; + } + + public void printDepartmentName() { + System.out.println(getClass().getSimpleName()); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/composite/GeneralDepartment.java b/core-java/src/main/java/com/baeldung/designpatterns/composite/GeneralDepartment.java new file mode 100644 index 0000000000..15c5bc9494 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/composite/GeneralDepartment.java @@ -0,0 +1,33 @@ +package com.baeldung.designpatterns.composite; + +import java.util.ArrayList; +import java.util.List; + +/** + * Created by Gebruiker on 5/1/2018. + */ +public class GeneralDepartment implements Department { + + private Integer id; + private String name; + + private List childDepartments; + + public GeneralDepartment(Integer id, String name) { + this.id = id; + this.name = name; + this.childDepartments = new ArrayList(); + } + + public void printDepartmentName() { + childDepartments.stream().forEach(Department::printDepartmentName); + } + + public void addDepartMent(Department department) { + childDepartments.add(department); + } + + public void removeDepartment(Department department) { + childDepartments.remove(department); + } +} diff --git a/core-java/src/main/java/com/baeldung/designpatterns/composite/SalesDepartment.java b/core-java/src/main/java/com/baeldung/designpatterns/composite/SalesDepartment.java new file mode 100644 index 0000000000..af234b4915 --- /dev/null +++ b/core-java/src/main/java/com/baeldung/designpatterns/composite/SalesDepartment.java @@ -0,0 +1,35 @@ +package com.baeldung.designpatterns.composite; + +/** + * Created by Gebruiker on 5/1/2018. + */ +public class SalesDepartment implements Department { + + private Integer id; + private String name; + + public SalesDepartment(Integer id, String name) { + this.id = id; + this.name = name; + } + + public void printDepartmentName() { + System.out.println(getClass().getSimpleName()); + } + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} From 4ad804252205ef5b0e6a0e60d4302a087917eceb Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Sat, 5 May 2018 10:26:51 +0400 Subject: [PATCH 2/3] test the results --- .../designpatterns/composite/CompositeDemo.java | 10 +++++++++- .../{GeneralDepartment.java => HeadDepartment.java} | 4 ++-- 2 files changed, 11 insertions(+), 3 deletions(-) rename core-java/src/main/java/com/baeldung/designpatterns/composite/{GeneralDepartment.java => HeadDepartment.java} (86%) diff --git a/core-java/src/main/java/com/baeldung/designpatterns/composite/CompositeDemo.java b/core-java/src/main/java/com/baeldung/designpatterns/composite/CompositeDemo.java index 83024b7d21..9537dd0d2b 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/composite/CompositeDemo.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/composite/CompositeDemo.java @@ -6,6 +6,14 @@ package com.baeldung.designpatterns.composite; public class CompositeDemo { public static void main(String args[]) { - Department salesDepartment = new SalesDepartment(); + Department salesDepartment = new SalesDepartment(1, "Sales department"); + Department financialDepartment = new FinancialDepartment(2, "Financial department"); + + HeadDepartment headDepartment = new HeadDepartment(3, "Head department"); + + headDepartment.addDepartMent(salesDepartment); + headDepartment.addDepartMent(financialDepartment); + + headDepartment.printDepartmentName(); } } diff --git a/core-java/src/main/java/com/baeldung/designpatterns/composite/GeneralDepartment.java b/core-java/src/main/java/com/baeldung/designpatterns/composite/HeadDepartment.java similarity index 86% rename from core-java/src/main/java/com/baeldung/designpatterns/composite/GeneralDepartment.java rename to core-java/src/main/java/com/baeldung/designpatterns/composite/HeadDepartment.java index 15c5bc9494..8854fce5b3 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/composite/GeneralDepartment.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/composite/HeadDepartment.java @@ -6,14 +6,14 @@ import java.util.List; /** * Created by Gebruiker on 5/1/2018. */ -public class GeneralDepartment implements Department { +public class HeadDepartment implements Department { private Integer id; private String name; private List childDepartments; - public GeneralDepartment(Integer id, String name) { + public HeadDepartment(Integer id, String name) { this.id = id; this.name = name; this.childDepartments = new ArrayList(); From 2a92926cad3c7502a4391b09713ddfde8e9e51a0 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Tue, 8 May 2018 09:15:51 +0400 Subject: [PATCH 3/3] remove stream --- .../com/baeldung/designpatterns/composite/HeadDepartment.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java/src/main/java/com/baeldung/designpatterns/composite/HeadDepartment.java b/core-java/src/main/java/com/baeldung/designpatterns/composite/HeadDepartment.java index 8854fce5b3..119b9c76b5 100644 --- a/core-java/src/main/java/com/baeldung/designpatterns/composite/HeadDepartment.java +++ b/core-java/src/main/java/com/baeldung/designpatterns/composite/HeadDepartment.java @@ -20,7 +20,7 @@ public class HeadDepartment implements Department { } public void printDepartmentName() { - childDepartments.stream().forEach(Department::printDepartmentName); + childDepartments.forEach(Department::printDepartmentName); } public void addDepartMent(Department department) {