接口型模式之Composite(组合)模式

本文介绍了一种常用的设计模式——组合模式,并通过一个具体的组织结构树的例子来展示如何使用该模式。组合模式允许客户端以一致的方式处理单个对象和组合对象,从而简化了高层模块的设计。文章还提供了一个简单的实现代码,展示了如何构建一个由公司部门组成的树形结构。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值