组合模式安全写法实现多级目录

1.安全写法规定相对于透明写法而言,顶层的抽象中只包含必要的细节。不用实现不要的方法,具体细节在子类实现中实现。
2.顶层抽象Root

public abstract class Root {

    String name;

    public Root(String name) {
        this.name = name;
    }

    public  abstract  void show();
}

分支节点

package com.dxy;

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

/**
 * @author dxy
 * @version 1.0
 * @date 2021/3/29 15:45
 * @Description 树枝节点可能又叶子和别的分支
 */
public class Branch extends Root {
    //节点等级
    private Integer level;
    //子节点集合
    private List<Root> subs;

    public Branch(String name, Integer level) {
        super(name);
        this.level = level;
        this.subs = new ArrayList();
    }

    @Override
    public void show() {
        System.out.println("====我是树枝节点--》name:=>" + this.name);
        subs.forEach(e -> {
            if (this.level != null) {
                for (int i = 0; i < this.level; i++) {
                    //显示格式前面空格
                    System.out.print(" ");
                }
                for (int i = 0; i < this.level; i++) {
                    if (i == 0) {
                        System.out.print("+");
                    }
                    System.out.print("-");
                }
            }
            e.show();
        });

    }

    public void addChild(Root child) {
        subs.add(child);
    }

    public void removeChild(Root child) {
        subs.remove(child);
    }

    public Root getOne(Integer i) {
        return this.subs.get(i);
    }

    public void list() {
        this.subs.forEach(e -> System.out.println(e.name));
    }
}

叶子节点

public class Leaf extends Root {

    public Leaf(String name) {
        super(name);
    }
    @Override
    public void show() {
        System.out.println("我是叶子-->name:" + this.name);
    }
}

测试方法

    public static void main(String[] args) {
        //叶子节点
        Leaf leafqq = new Leaf("qq.exe");
        Leaf leafwechat = new Leaf("wechat.exe");
        Branch branch1 = new Branch("聊天工具",2);
        branch1.addChild(leafqq);
        branch1.addChild(leafwechat);

        Leaf idea = new Leaf("alipay.exe");
        Leaf nodepad = new Leaf("baidu.exe");
        Leaf eclipse = new Leaf("sina.exe");
        Branch branch2 = new Branch("开发工具",2);
        branch2.addChild(idea);
        branch2.addChild(nodepad);
        branch2.addChild(eclipse);

        Leaf pdf = new Leaf("pdf.exe");
        Leaf ppt = new Leaf("ppt.exe");
        Leaf wps = new Leaf("wps.exe");

        Branch branch3 = new Branch("办公软件",2);
        Branch branch4 = new Branch("国产软件",3);
        Branch branch5 = new Branch("非国产软件",3);

        branch4.addChild(wps);
        branch5.addChild(pdf);
        branch5.addChild(ppt);
        branch3.addChild(branch4);
        branch3.addChild(branch5);

        //根
        Branch root = new Branch("根目录",1);
        root.addChild(branch1);
        root.addChild(branch2);
        root.addChild(branch3);
        root.show();
    }

结果

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

焱童鞋

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值