Friday, February 13, 2009

Sample of Java Pattern -- Composite

Department.java
package composite;

import java.util.List;

public interface Department {
public Department getDepartment();
public String getDepartmentname();
public void add(Department department);
public void remove(Department department);
public boolean hasSubDepartments();
public List<Department> getSubDepartments();
}


CompositeDepartment.java
package composite;

import java.util.ArrayList;
import java.util.List;

public class CompositeDepartment implements Department {
private String name;
private List<Department> departments = new ArrayList<Department>();

public CompositeDepartment(String name) {
super();
System.out.println("Department " + name + " is created.");
this.name = name;
}

public Department getDepartment() {
return this;
}

public String getDepartmentname() {
return this.name;
}

public void add(Department department){
System.out.println("Department " + department.getDepartmentname() + " is added to Department " + this.name + ".");
departments.add(department);
}

public void remove(Department department){
System.out.println("Department " + department.getDepartmentname() + " is removed from Department " + this.name + ".");
departments.remove(department);
}

public boolean hasSubDepartments(){
if(departments == null || departments.isEmpty())
return false;
return true;
}

public List<Department> getSubDepartments(){
if(hasSubDepartments())
return departments;
return null;
}
}


SingleDepartment.java
package composite;

import java.util.List;

public class SingleDepartment implements Department {
private String name;

public SingleDepartment(String name) {
super();
System.out.println("Department " + name + " is created.");
this.name = name;
}

public Department getDepartment() {
return this;
}

public String getDepartmentname() {
return this.name;
}

public void add(Department department){
System.out.println("Could not add a child department in Department " + this.name + ".");
}

public void remove(Department department){
System.out.println("Department " + department.getDepartmentname() + " doesn't belong to Department " + this.name + ".");
}

public boolean hasSubDepartments(){
return false;
}

public List<Department> getSubDepartments(){
return null;
}
}


CompositeTest.java
package composite;

public class CompositeTest {
public static void main(String[] args) {
Department hq = new CompositeDepartment("HQ");
Department financing = new SingleDepartment("Financing");
Department marketing = new CompositeDepartment("Marketing");
Department preSales = new SingleDepartment("PreSales");
Department sales = new SingleDepartment("Sales");
Department postSales = new SingleDepartment("PostSales");
Department techSupport = new SingleDepartment("TechSupport");
hq.add(financing);
hq.add(marketing);
hq.add(techSupport);
marketing.add(preSales);
marketing.add(sales);
marketing.add(postSales);
System.out.println("hq has sub departments: " + hq.hasSubDepartments());
System.out.println("marketing has sub departments: " + marketing.hasSubDepartments());
System.out.println("techSupport has sub departments: " + techSupport.hasSubDepartments());
marketing.remove(preSales);
marketing.remove(sales);
marketing.remove(postSales);
sales.remove(preSales);
hq.remove(financing);
hq.remove(marketing);
hq.remove(techSupport);
}
}


you may download the source code.

No comments:

Post a Comment