1.如果你期望为单个对象和群组对象定义共同的接口,可使用Composite(组合)模式
2.举例:
2.1uml模型
2.2源码
//抽象节点
public abstract class Company {
private String name;
public Company(String name) {
this.name = name;
}
public Company() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
protected abstract void add(Company company);
protected abstract void romove(Company company);
protected abstract void display(int depth);
}
//子节点
public class HRDepartment extends Company {
public HRDepartment(){
}
public HRDepartment(String name){
super(name);
}
@Override
protected void add(Company company) {
}
@Override
protected void display(int depth) {
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < depth; i++) {
sb.append("-");
}
System.out.println(new String(sb) + this.getName() ) ;
}
@Override
protected void romove(Company company) {
}
}
//子节点
public class FinanceDepartment extends Company {
public FinanceDepartment(){
}
public FinanceDepartment(String name){
super(name);
}
@Override
protected void add(Company company) {
}
@Override
protected void display(int depth) {
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < depth; i++) {
sb.append("-");
}
System.out.println(new String(sb) + this.getName() ) ;
}
@Override
protected void romove(Company company) {
}
}
import java.util.ArrayList;
import java.util.List;
//根节点
public class ConcreteCompany extends Company {
private List<Company> cList;
public ConcreteCompany() {
cList = new ArrayList<Company>();
}
public ConcreteCompany(String name) {
super(name);
cList = new ArrayList<Company>() ;
}
@Override
protected void add(Company company) {
cList.add(company);
}
@Override
protected void display(int depth) {
// TODO Auto-generated method stub
StringBuilder sb = new StringBuilder("");
for (int i = 0; i < depth; i++) {
sb.append("-");
}
System.out.println(new String(sb) + this.getName());
for (Company c : cList) {
c.display(depth + 2);
}
}
@Override
protected void romove(Company company) {
cList.remove(company);
}
}
//测试类
public class Client {
public static void main(String[] args) {
// TODO Auto-generated method stub
Company root = new ConcreteCompany();
root.setName("A0");
root.add(new HRDepartment("B0"));
root.add(new FinanceDepartment("B1"));
Company B2 = new ConcreteCompany("B2");
B2.add(new HRDepartment("C0"));
B2.add(new FinanceDepartment("C1"));
Company C2 = new ConcreteCompany("C2");
C2.add(new FinanceDepartment("D0"));
C2.add(new HRDepartment("D1"));
B2.add(C2);
Company C3 = new ConcreteCompany("C3");
C3.add(new FinanceDepartment("D0"));
C3.add(new HRDepartment("D1"));
B2.add(C3);
Company B3 = new ConcreteCompany("B3");
B3.add(new HRDepartment("C0"));
B3.add(new FinanceDepartment("C1"));
Company B3C2 = new ConcreteCompany("B3C2");
B3C2.add(new FinanceDepartment("B3C2D0"));
B3C2.add(new HRDepartment("B3C2D1"));
B3.add(B3C2);
Company B3C3 = new ConcreteCompany("B3C3");
B3C3.add(new FinanceDepartment("B3C3D0"));
B3C3.add(new HRDepartment("B3C3D1"));
B3.add(B3C3);
root.add(B2);
root.add(B3);
root.display(0);
}
}
4.结果:
A0
--B0
--B1
--B2
----C0
----C1
----C2
------D0
------D1
----C3
------D0
------D1
--B3
----C0
----C1
----B3C2
------B3C2D0
------B3C2D1
----B3C3
------B3C3D0
------B3C3D1
5.总结:Composite模式包含两个相关的重要概念。其中一个概念是群组可以包含个体对象,也可以包含其他的群组,与此相关的另一个概念是群组和个体对象可以共享同一个接口。
6.参考:http://blog.csdn.net/hfmbook/article/details/7693069