组合模式
将对象组合成树形的结构形式表示“整体-部分”的层次结构,组合模式使得单个对象和组合对象的使用具有一致性。
模型图
代码
public abstract class Component {
protected String name;
public Component(String name) {
this.name=name;
}
public abstract void add(Component c);
public abstract void remove(Component c);
public abstract void show(String str);
}
public class Composite extends Component {
Collection<Component> components=Collections.synchronizedList(new ArrayList<Component>());
public Composite(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
public void add(Component c) {
components.add(c);
}
@Override
public void remove(Component c) {
components.remove(c);
}
@Override
public void show(String str) {
System.out.println(str+name);
for(Component component:components){
component.show("-"+str);
}
}}
public class Leaf extends Component{
public Leaf(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
public void add(Component c) {
//叶子节点不做实现
}
@Override
public void remove(Component c) {
//叶子节点不做实现
}
@Override
public void show(String str) {
System.out.println(str+name);
}
}
public class Test {
public static void main(String[] args) {
Composite root=new Composite("root");
Composite composite2=new Composite("X");
composite2.add(new Leaf("XA"));
composite2.add(new Leaf("XB"));
root.add(composite2);
Composite composite3=new Composite("Y");
Composite composite4=new Composite("C");
composite4.add(new Leaf("YAA"));
composite3.add(new Leaf("YA"));
composite3.add(composite4);
root.add(composite3);
root.add(new Leaf("Z"));
root.show("");
}
}
运行结果图
案例 公司要求创建不同的部分,这些部门除了名字与职能不同外,其他的构造组织都相同
分析与设计 整体与部分的层级的关系 菜单 公司组织架构 管理团队层次架构
模型图
代码
public abstract class Company {
protected String name;
public Company(String name) {
this.name=name;
}
public abstract void add(Company c);
public abstract void remove(Company c);
public abstract void show(String str);
}
public class ConcreteCompany extends Company{
Collection<Company> components=Collections.synchronizedList(new ArrayList<Company>());
public ConcreteCompany(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
public void add(Company c) {
// TODO Auto-generated method stub
components.add(c);
}
@Override
public void remove(Company c) {
// TODO Auto-generated method stub
components.add(c);
}
@Override
public void show(String str) {
System.out.println(str+name);
for(Company component:components){
component.show("-"+str);
}
}
}
public class FinanceCompany extends Company {
public FinanceCompany(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
public void add(Company c) {
// TODO Auto-generated method stub
}
@Override
public void remove(Company c) {
// TODO Auto-generated method stub
}
@Override
public void show(String str) {
System.out.println(str+name);
}
}
public class HrCompany extends Company {
public HrCompany(String name) {
super(name);
// TODO Auto-generated constructor stub
}
@Override
public void add(Company c) {
// TODO Auto-generated method stub
}
@Override
public void remove(Company c) {
// TODO Auto-generated method stub
}
@Override
public void show(String str) {
// TODO Auto-generated method stub
System.out.println(str+name);
}
}
public class Test {
public static void main(String[] args) {
ConcreteCompany root=new ConcreteCompany("北京总部");
ConcreteCompany concreteCompany2=new ConcreteCompany("上海分公司");
FinanceCompany financeCompany=new FinanceCompany("上海东部财政部");
HrCompany hrCompany=new HrCompany("上海东部人力资源部");
concreteCompany2.add(financeCompany);
concreteCompany2.add(hrCompany);
ConcreteCompany concreteCompany3=new ConcreteCompany("广州分公司");
FinanceCompany financeCompany2=new FinanceCompany("广州西部财政部");
HrCompany hrCompany2=new HrCompany("广州西部人力资源部");
concreteCompany3.add(financeCompany2);
concreteCompany3.add(hrCompany2);
root.add(concreteCompany2);
root.add(concreteCompany3);
root.show("-");
}
}
结果图
小结
在需要体现部分与整体的层次结构时,而且每一个部分又都是一个可以独立存在的个体。
类似树结构模型的需求的时候