通知 网站从因情语写改为晴雨,这个网站的模板也从calmlog_ex改为 whimurmur

结构型模式-组合模式

5134人浏览 / 0人评论 / | 作者:因情语写  | 分类: 设计模式与算法  | 标签: 设计模式与算法

作者:因情语写

链接:https://www.qingyu.blue/article/445

声明:请尊重原作者的劳动,如需转载请注明出处


  组合模式可以处理整体和部分的关系,如果整体和部分拥有相同的结构,最典型的如二叉树,文件系统

  下面来看例子,公司,分公司,部门

package constructional.pattern.composite;

/**
 * 文件名称:design.composite.Company.java
 *  创建人:Fei Wong 
 *  创建时间: 2012-06-26
 * 电子邮箱:feiwong8@126.com 
 * 
 */
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 constructional.pattern.composite;

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

/**
 * 文件名称:design.singleton.CommandInvoker.java
 *  创建人:Fei Wong 
 *  创建时间: 2012-06-26
 * 电子邮箱:feiwong8@126.com 
 * 
 */
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);
    }
}
package constructional.pattern.composite;

/**
 * 文件名称:design.composite.FinanceDepartment.java
 *  创建人:Fei Wong 
 *  创建时间: 2012-06-26
 * 电子邮箱:feiwong8@126.com
 */
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) {
        
    }
    
}
package constructional.pattern.composite;

/**
 * 文件名称:design.composite.HRDepartment.java
 *  创建人:Fei Wong 
 *  创建时间: 2012-06-26
 * 电子邮箱:feiwong8@126.com
 */
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 constructional.pattern.composite;

public class Client {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Company root = new ConcreteCompany();
        root.setName("北京总公司");
        root.add(new HRDepartment("总公司人力资源部"));
        root.add(new FinanceDepartment("总公司财务部"));
        Company shandongCom = new ConcreteCompany("山东分公司");
        shandongCom.add(new HRDepartment("山东分公司人力资源部"));
        shandongCom.add(new FinanceDepartment("山东分公司账务部"));
        Company zaozhuangCom = new ConcreteCompany("枣庄办事处");
        zaozhuangCom.add(new FinanceDepartment("枣庄办事处财务部"));
        zaozhuangCom.add(new HRDepartment("枣庄办事处人力资源部"));
        Company jinanCom = new ConcreteCompany("济南办事处");
        jinanCom.add(new FinanceDepartment("济南办事处财务部"));
        jinanCom.add(new HRDepartment("济南办事处人力资源部")); 
        shandongCom.add(jinanCom);
        shandongCom.add(zaozhuangCom);
        Company huadongCom = new ConcreteCompany("上海华东分公司");
        huadongCom.add(new HRDepartment("上海华东分公司人力资源部"));
        huadongCom.add(new FinanceDepartment("上海华东分公司账务部"));
        Company hangzhouCom = new ConcreteCompany("杭州办事处");
        hangzhouCom.add(new FinanceDepartment("杭州办事处财务部"));
        hangzhouCom.add(new HRDepartment("杭州办事处人力资源部"));
        Company nanjingCom = new ConcreteCompany("南京办事处");
        nanjingCom.add(new FinanceDepartment("南京办事处财务部"));
        nanjingCom.add(new HRDepartment("南京办事处人力资源部"));
        huadongCom.add(hangzhouCom);
        huadongCom.add(nanjingCom); 
        root.add(shandongCom);
        root.add(huadongCom);
        root.display(0);
    }
}

  运行结果如下:


自己写的文章声明标题示例:


点赞(0) 打赏

全部评论

还没有评论!