Composite design pattern
This commit is contained in:
parent
13d226c97b
commit
9a496e0d25
@ -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();
|
||||||
|
}
|
||||||
|
}
|
@ -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 GeneralDepartment implements Department {
|
||||||
|
|
||||||
|
private Integer id;
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
private List<Department> childDepartments;
|
||||||
|
|
||||||
|
public GeneralDepartment(Integer id, String name) {
|
||||||
|
this.id = id;
|
||||||
|
this.name = name;
|
||||||
|
this.childDepartments = new ArrayList<Department>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void printDepartmentName() {
|
||||||
|
childDepartments.stream().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…
x
Reference in New Issue
Block a user