File tree Expand file tree Collapse file tree
core-java/src/main/java/com/baeldung/designpatterns/composite Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package com .baeldung .designpatterns .composite ;
2+
3+ /**
4+ * Created by Gebruiker on 5/3/2018.
5+ */
6+ public class CompositeDemo {
7+
8+ public static void main (String args []) {
9+ Department salesDepartment = new SalesDepartment (1 , "Sales department" );
10+ Department financialDepartment = new FinancialDepartment (2 , "Financial department" );
11+
12+ HeadDepartment headDepartment = new HeadDepartment (3 , "Head department" );
13+
14+ headDepartment .addDepartMent (salesDepartment );
15+ headDepartment .addDepartMent (financialDepartment );
16+
17+ headDepartment .printDepartmentName ();
18+ }
19+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .designpatterns .composite ;
2+
3+ /**
4+ * Created by Gebruiker on 5/1/2018.
5+ */
6+ public interface Department {
7+
8+ void printDepartmentName ();
9+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .designpatterns .composite ;
2+
3+ /**
4+ * Created by Gebruiker on 5/1/2018.
5+ */
6+ public class FinancialDepartment implements Department {
7+
8+ private Integer id ;
9+ private String name ;
10+
11+ public FinancialDepartment (Integer id , String name ) {
12+ this .id = id ;
13+ this .name = name ;
14+ }
15+
16+ public void printDepartmentName () {
17+ System .out .println (getClass ().getSimpleName ());
18+ }
19+
20+ public Integer getId () {
21+ return id ;
22+ }
23+
24+ public void setId (Integer id ) {
25+ this .id = id ;
26+ }
27+
28+ public String getName () {
29+ return name ;
30+ }
31+
32+ public void setName (String name ) {
33+ this .name = name ;
34+ }
35+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .designpatterns .composite ;
2+
3+ import java .util .ArrayList ;
4+ import java .util .List ;
5+
6+ /**
7+ * Created by Gebruiker on 5/1/2018.
8+ */
9+ public class HeadDepartment implements Department {
10+
11+ private Integer id ;
12+ private String name ;
13+
14+ private List <Department > childDepartments ;
15+
16+ public HeadDepartment (Integer id , String name ) {
17+ this .id = id ;
18+ this .name = name ;
19+ this .childDepartments = new ArrayList <Department >();
20+ }
21+
22+ public void printDepartmentName () {
23+ childDepartments .forEach (Department ::printDepartmentName );
24+ }
25+
26+ public void addDepartMent (Department department ) {
27+ childDepartments .add (department );
28+ }
29+
30+ public void removeDepartment (Department department ) {
31+ childDepartments .remove (department );
32+ }
33+ }
Original file line number Diff line number Diff line change 1+ package com .baeldung .designpatterns .composite ;
2+
3+ /**
4+ * Created by Gebruiker on 5/1/2018.
5+ */
6+ public class SalesDepartment implements Department {
7+
8+ private Integer id ;
9+ private String name ;
10+
11+ public SalesDepartment (Integer id , String name ) {
12+ this .id = id ;
13+ this .name = name ;
14+ }
15+
16+ public void printDepartmentName () {
17+ System .out .println (getClass ().getSimpleName ());
18+ }
19+
20+ public Integer getId () {
21+ return id ;
22+ }
23+
24+ public void setId (Integer id ) {
25+ this .id = id ;
26+ }
27+
28+ public String getName () {
29+ return name ;
30+ }
31+
32+ public void setName (String name ) {
33+ this .name = name ;
34+ }
35+ }
You can’t perform that action at this time.
0 commit comments