设计模式之组合模式

需求:在一个页面展示学校下面的学院,学院下面所有的专业信息。

如果我们按时学校->学院->专业这样一层一层的划分,然后分别写出对应的类信息,这样写出来的代码类比较多且大,而且也不能很好的展示学校的新,如果我们把这样结构划分为一棵树结果,学校是根节点,然后一层一层的划分,最下面的专业是叶子节点,这样我们就能很友好的管理树信息。====》组合模式来解决该问题。

组合模式:组合模式又叫部分和整体的模式,他创建对象组的树形结构,将对象组合树形结构已整体-部分的层次关系来展示。

组合模式依据树形结构来组合对象,用来表示部分以及整体层次,该设计模式属于结构型模式,组合模式使得用户对单个对象和组合对象的访问具有一致性,即:组合能让客户以一致性的放肆处理个别对象以及组合对象。

通过代码来解决学校展示的问题:

类图信息:

 

 

/**
 * 定义公共抽象类,定义规范信息
 * @author Administrator
 *
 */
public abstract class OrganizationComponent {

    //学校名称
    private String name;
    
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public OrganizationComponent(String name) {
        
        this.name = name;
    }
    
    /**
     * 添加的方法
     * @param organizationComponent
     */
    protected void add(OrganizationComponent organizationComponent) {
        
    }
    
    /**
     * 删除学校的方法
     * @param organizationComponent
     */
    protected void remove(OrganizationComponent organizationComponent) {
        
    }
    /**
     * 打印信息
     */
    protected abstract void print();
    
}

 

 

 

 

/**
 * 学校的信息
 * @author Administrator
 *
 */
public class University extends OrganizationComponent{
    
    List<OrganizationComponent> lists = new ArrayList<OrganizationComponent>();

    public University(String name) {
        super(name);
    }

    @Override
    protected void add(OrganizationComponent organizationComponent) {
        lists.add(organizationComponent);
    }

    @Override
    protected void remove(OrganizationComponent organizationComponent) {
        lists.remove(organizationComponent);
    }


    @Override
    protected void print() {
        System.out.println("-------------"+this.getName()+"-------------------");
        for (OrganizationComponent list : lists) {
            list.print();
        }
    }

}
 

 

 

 

/**
 * 学院的基本信息
 * @author Administrator
 *
 */
public class College extends OrganizationComponent{
    
    List<OrganizationComponent> lists = new ArrayList<OrganizationComponent>();

    public College(String name) {
        super(name);
    }

    
    @Override
    protected void add(OrganizationComponent organizationComponent) {
        lists.add(organizationComponent);
    }

    @Override
    protected void remove(OrganizationComponent organizationComponent) {
        lists.remove(organizationComponent);
    }
    
    @Override
    protected void print() {
        System.out.println("--" + this.getName() + "--");
        for (OrganizationComponent list : lists) {
            list.print();
        }
    }

}
 

 

 

 

 

/**
 * 专业信息
 * @author Administrator
 *
 */
public class Major extends OrganizationComponent {

    public Major(String name) {
        super(name);
    }

    @Override
    protected void print() {
        System.out.println(this.getName());
    }

}

 

 

/**
 * 展示学校的信息
 * @author Administrator
 *
 */
public class Client {

    public static void main(String[] args) {
        OrganizationComponent university = new University("东北大学");
        
        OrganizationComponent college1 = new College("计算机学院");
        OrganizationComponent college2 = new College("生物工程学院");
        university.add(college1);
        university.add(college2);
        
        OrganizationComponent major1 = new Major("软件工程");
        OrganizationComponent major2 = new Major("网络工程");
        college1.add(major1);
        college1.add(major2);
        
        OrganizationComponent major3 = new Major("生物工程");
        OrganizationComponent major4 = new Major("生物科学");
        college2.add(major3);
        college2.add(major4);
        
        university.print();
    }
}

 

组合模式注意事项:

  • 简化客户端操作,客户单只需要面对一致性的对象而不用考虑整体部分或者节点叶子问题。
  • 具有较强的扩展性,我们需要更改组合的时候,我们只需要调动内部的层次关系,而不需要更改业务代码。
  • 方便创建出复杂的层次结构,客户端不用理会组合里面的组成细节,容易添加节点或者从叶子节点创建出复杂的树形结构。
  • 树形结构的遍历比较方便,但是要求节点和叶子节点的差异性不能很大,如果每个类的方法和属性不一样,不适合使用组合模式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值