结构型-组合模式

组合模式

组合模式又叫部分整体模式,创建了对象组的树形结构。组合模式是依据树形结构来组合对象,用来表示部分以及整体的层次关系。

其实就是一对多的关系,只不过将所有的类抽行成了实体类(所有类都有的是抽象方法,其他的则是普同方法)

问题

实现学校下面的学院里面的系

代码实现

首先将三者的共同点抽成一个抽象类。


/**
 * @program: sheji
 * @description: 将所有下级节点抽象成一个抽象类(适用于树形结构)
 * @author: wfg
 * @create: 2021-05-12 18:17
 */
@Data
@AllArgsConstructor
public abstract class OrganizationComponent {
    /**
     * @Description: 名字
     * @Author: wfg
     */
    private String name;
    /**
     * @Description: 描述
     * @Author: wfg
     */
    private String des;

    /**
     * @Description: add 增加下一级节点,这里不使用抽象类的原因是因为叶子节点不需要重写实现add
     * @Author: wfg
     */
    protected   void add(OrganizationComponent organizationComponent) throws OperationNotSupportedException {
         throw new OperationNotSupportedException();
     }
    /**
     * @Description: remove 删除下一级节点,这里不使用抽象类的原因是因为叶子节点不需要重写实现remove
     * @Author: wfg
     */
    protected   void remove(OrganizationComponent organizationComponent) throws OperationNotSupportedException {
        throw new OperationNotSupportedException();
    }

    /**
     * @Description: 抽象方法(子类必须都实现)
     * @Author: wfg
     */
   protected abstract void print();

}

第一级学校的实现


/**
 * @program: sheji
 * @description: 第一级 学校
 * @author: wfg
 * @create: 2021-05-12 18:31
 */
public class University extends OrganizationComponent{
    /**
     * @Description:  组合方式 学校里面包含许多学院
     * @Author: wfg
     */
    List<OrganizationComponent> organizationComponents = new ArrayList<>();

    /**
     * @Description: 构造器
     * @Author: wfg
     */
    public   University (String name,String des){
        super(name, des);
    }

    /**
     * @Description:  重写方法 在学校集合里面增加新的学院
     * @Author: wfg
     */
    @Override
    protected void add(OrganizationComponent organizationComponent) throws OperationNotSupportedException {
       organizationComponents.add(organizationComponent);
    }
   /**
    * @Description: 重写删除方法 删除集合里面的学院
    * @Author: wfg
    */
   @Override
    protected void remove(OrganizationComponent organizationComponent) throws OperationNotSupportedException {
        organizationComponents.remove(organizationComponent);
    }

    @Override
    protected void print() {
      for(OrganizationComponent oc: organizationComponents){
         oc.print();
      }
    }
}

第二级 学院

/**
 * @program: sheji
 * @description: 第二级 学院
 * @author: wfg
 * @create: 2021-05-12 19:41
 */
public class College extends OrganizationComponent{
    /**
     * @Description: 采用聚合方式 存放department(叶子)
     * @Author: wfg
     */
    List<OrganizationComponent> organizationComponents =new ArrayList<>();

    /**
     * @Description:  构造器,子类拥有父类private属性,但是无法使用
     * @Author: wfg
     */
    public  College(String name,String des){
        super(name, des);
    }

    /**
     * @Description:  重写方法
     * @Author: wfg
     */
    @Override
    protected void add(OrganizationComponent organizationComponent) throws OperationNotSupportedException {
        organizationComponents.add(organizationComponent);
    }
    /**
     * @Description: 重写删除方法
     * @Author: wfg
     */
    @Override
    protected void remove(OrganizationComponent organizationComponent) throws OperationNotSupportedException {
        organizationComponents.remove(organizationComponent);
    }

    @Override
    protected void print() {
        for(OrganizationComponent oc: organizationComponents){
          oc.print();
        }
    }
}

第三级 系

/**
 * @program: sheji
 * @description: 叶子(最终无需写集合以及add,remove方法)
 * @author: wfg
 * @create: 2021-05-12 19:49
 */
public class Department extends OrganizationComponent{
    /**
     * @Description: 构造器
     * @Author: wfg
     */
    public Department(String name,String des){
        super(name, des);
    }

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

客户端的调用

/**
 * @program: sheji
 * @description:
 * @author: wfg
 * @create: 2021-05-12 17:44
 */
public class Test {
    public static void main(String[] args) throws OperationNotSupportedException {
        //从上往下 开始创建对象
        OrganizationComponent hpu = new University("河南理工大学","双非一本");
        //创建学院
        OrganizationComponent c1=new College("计算计科学与技术","一般般");
        OrganizationComponent c2=new College("机械","jixie");
        //创建叶子(系)
        OrganizationComponent d1= new Department("计科合","111");
        OrganizationComponent d2= new Department("计科","222");

        OrganizationComponent d3= new Department("机电","333");
        OrganizationComponent d4= new Department("机械","444");
        //进行增加
        hpu.add(c1);
        hpu.add(c2);

        c1.add(d1);
        c1.add(d2);

        c2.add(d3);
        c2.add(d4);

        hpu.print();
    }
}

注意事项

此模式适用于处理的对象具有树形结构

如果节点和叶子具有很多的差异性的话(很多属性和方法都不一样)就不适合使用组合模式

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值