From 9a496e0d258c8d125ee6b62da8bdc69fbe75eab4 Mon Sep 17 00:00:00 2001 From: mherbaghinyan Date: Thu, 3 May 2018 12:13:12 +0400 Subject: [PATCH] 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; + } +}