commit
afd7079968
|
@ -0,0 +1,19 @@
|
|||
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(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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
package com.baeldung.designpatterns.composite;
|
||||
|
||||
/**
|
||||
* Created by Gebruiker on 5/1/2018.
|
||||
*/
|
||||
public interface Department {
|
||||
|
||||
void printDepartmentName();
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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 HeadDepartment implements Department {
|
||||
|
||||
private Integer id;
|
||||
private String name;
|
||||
|
||||
private List<Department> childDepartments;
|
||||
|
||||
public HeadDepartment(Integer id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.childDepartments = new ArrayList<Department>();
|
||||
}
|
||||
|
||||
public void printDepartmentName() {
|
||||
childDepartments.forEach(Department::printDepartmentName);
|
||||
}
|
||||
|
||||
public void addDepartMent(Department department) {
|
||||
childDepartments.add(department);
|
||||
}
|
||||
|
||||
public void removeDepartment(Department department) {
|
||||
childDepartments.remove(department);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue