Merge pull request #4177 from MherBaghinyan/BAEL-1744

Bael 1744
This commit is contained in:
Loredana Crusoveanu 2018-05-08 22:08:23 +03:00 committed by GitHub
commit afd7079968
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 131 additions and 0 deletions

View File

@ -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();
}
}

View File

@ -0,0 +1,9 @@
package com.baeldung.designpatterns.composite;
/**
* Created by Gebruiker on 5/1/2018.
*/
public interface Department {
void printDepartmentName();
}

View File

@ -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;
}
}

View File

@ -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);
}
}

View File

@ -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;
}
}