组合模式:将对象组合成树形结构以表示“部分整体”的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性【DP】
Company:
package com.sjmx.composite; 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); }
叶子节点:
package com.sjmx.composite; public class LeafCompany extends Company { public LeafCompany() { } public LeafCompany(String name) { super(name); } @Override protected void add(Company company) { } @Override protected void romove(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()); } }
直属节点:
package com.sjmx.composite; 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) {} }
树干节点:
package com.sjmx.composite; 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) { 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 + 4); } } @Override protected void romove(Company company) { cList.remove(company); } }
客户端:
package com.sjmx.composite; public class Client { /** * @param args */ public static void main(String[] args) { Company root = new ConcreteCompany(); root.setName("北京总公司"); root.add(new HRDepartment("总公司人力资源部")); root.add(new ConcreteCompany("总公司财务部")); Company shandongCom = new ConcreteCompany("山东分公司"); shandongCom.add(new HRDepartment("山东分公司人力资源部")); shandongCom.add(new ConcreteCompany("山东分公司账务部")); Company zaozhuangCom = new ConcreteCompany("枣庄办事处"); zaozhuangCom.add(new ConcreteCompany("枣庄办事处财务部")); zaozhuangCom.add(new HRDepartment("枣庄办事处人力资源部")); Company jinanCom = new ConcreteCompany("济南办事处"); jinanCom.add(new ConcreteCompany("济南办事处财务部")); jinanCom.add(new HRDepartment("济南办事处人力资源部")); shandongCom.add(jinanCom); shandongCom.add(zaozhuangCom); Company huadongCom = new ConcreteCompany("上海华东分公司"); huadongCom.add(new HRDepartment("上海华东分公司人力资源部")); huadongCom.add(new ConcreteCompany("上海华东分公司账务部")); Company hangzhouCom = new ConcreteCompany("杭州办事处"); hangzhouCom.add(new ConcreteCompany("杭州办事处财务部")); hangzhouCom.add(new HRDepartment("杭州办事处人力资源部")); Company nanjingCom = new ConcreteCompany("南京办事处"); nanjingCom.add(new ConcreteCompany("南京办事处财务部")); nanjingCom.add(new HRDepartment("南京办事处人力资源部")); huadongCom.add(hangzhouCom); huadongCom.add(nanjingCom); root.add(shandongCom); root.add(huadongCom); root.display(0); } }
运行结果:
北京总公司 ----总公司人力资源部 ----总公司财务部 ----山东分公司 --------山东分公司人力资源部 --------山东分公司账务部 --------济南办事处 ------------济南办事处财务部 ------------济南办事处人力资源部 --------枣庄办事处 ------------枣庄办事处财务部 ------------枣庄办事处人力资源部 ----上海华东分公司 --------上海华东分公司人力资源部 --------上海华东分公司账务部 --------杭州办事处 ------------杭州办事处财务部 ------------杭州办事处人力资源部 --------南京办事处 ------------南京办事处财务部 ------------南京办事处人力资源部