安全组合模式

需求:计算机的文件系统是一个典型的树形结构,目录包含文件夹和文件,文件夹里面又可以包含文件夹和文件。下面用代码来实现一个目录系统。

UML图设计:

代码:

package test;

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

/**
 * Created by IntelliJ IDEA
 * User:chenweipeng
 * Date:3/29/2022
 * Time:2:59 PM
 * Description:
 */
public abstract class Directory {
    protected String name;

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

    public abstract void show();
}


class File extends Directory{

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

    @Override
    public void show() {
        System.out.println(this.name);
    }
}

class Folder extends Directory{

    private List<Directory> dirs;

    private Integer level;

    public Folder(String name, Integer level) {
        super(name);
        this.level = level;
        this.dirs = new ArrayList<Directory>();
    }

    @Override
    public void show() {
        System.out.println(this.name);
        for (Directory dir:dirs){
            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("-");
                }
            }
            dir.show();
        }
    }

    public boolean add(Directory dir){
        return this.dirs.add(dir);
    }

    public boolean remove(Directory dir){
        return this.dirs.remove(dir);
    }

    public Directory get(int index){
        return this.dirs.get(index);
    }

    public void list(){
        for (Directory dir:this.dirs){
            System.out.println(dir.name);
        }
    }
}

class Test11{
    public static void main(String[] args) {
        System.out.println("====================安全组合模式======================");
        File qq = new File("qq.exe");
        File wx = new File("wechat.exe");

        Folder office = new Folder("办公软件", 2);

        File word = new File("Word.exe");
        File ppt = new File("PowerPoint.exe");
        File excel = new File("Excel.exe");

        office.add(word);
        office.add(ppt);
        office.add(excel);

        Folder wps = new Folder("金沙软件", 2);
        wps.add(new File("WPS.exe"));
        office.add(wps);

        Folder root = new Folder("根目录", 1);
        root.add(qq);
        root.add(wx);
        root.add(office);

        System.out.println("-------------------show()方法效果------------------");
        root.show();

        System.out.println("-------------------list()方法效果-------------------");
        root.list();

    }
}

输出:

====================安全组合模式======================
-------------------show()方法效果------------------
根目录
 +-qq.exe
 +-wechat.exe
 +-办公软件
  +--Word.exe
  +--PowerPoint.exe
  +--Excel.exe
  +--金沙软件
  +--WPS.exe
-------------------list()方法效果-------------------
qq.exe
wechat.exe
办公软件

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值