使用安全组合模式实现无限级文件系统
计算机是程序员每天都要接触的,计算机里的文件系统就是一个典型的树形结构,目录包含文件夹和文件,文件夹里又可以包含文件夹和文件。本节主要来实现一个简单的文件系统。
文件系统有两个大层次,即文件夹和文件。其中,文件夹能容纳其他层次,为树枝节点。文件是最小单位,为叶子节点。由于文件系统层次较少,树枝节点(文件夹)结构相对稳定,且文件可以有很多类型,所以我们选择使用安全组合模式来实现文件系统,可以避免为叶子节点类型(文件)引入冗余方法。
首先创建顶层的抽象组件 Directory 类。
文件系统有两个大层次,即文件夹和文件。其中,文件夹能容纳其他层次,为树枝节点。文件是最小单位,为叶子节点。由于文件系统层次较少,树枝节点(文件夹)结构相对稳定,且文件可以有很多类型,所以我们选择使用安全组合模式来实现文件系统,可以避免为叶子节点类型(文件)引入冗余方法。
首先创建顶层的抽象组件 Directory 类。
public abstract class Directory {
protected String name;
public Directory(String name) {
this.name = name;
}
public abstract void show();
}
然后分别创建 File 类和 Folder 类,File 类的代码如下。
public class File extends Directory {
public File(String name) {
super(name);
}
public void show() {
System.out.println(this.name);
}
}
Folder 类的代码如下。
public 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>();
}
public void show() {
System.out.println(this.name);
for (Directory c : 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("-");
}
}
c.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 : dirs) {
System.out.println(dir.name);
}
}
}
可以看到,Folder 类不仅覆盖了顶层(父类)的 show() 方法,还增加了 list() 方法。最后我们来编写客户端测试代码。
public class Test {
public static void main(String[] args) {
System.out.println("==========安全组合模式=============");
Folder im = new Folder("即时聊天", 2);
File qq = new File("QQ.exe");
File wx = new File("微信.exe");
Folder sns = new Folder("社交", 3);
Folder love = new Folder("两性", 4);
Folder normal = new Folder("职场", 4);
Folder high = new Folder("高端", 5);
Folder low = new Folder("低端", 5);
File momo = new File("Momo.exe");
love.add(momo);
File maimai = new File("Maimai.exe");
low.add(maimai);
File boss = new File("Boss直聘.exe");
high.add(boss);
normal.add(high);
normal.add(low);
sns.add(love);
sns.add(normal);
im.add(qq);
im.add(wx);
im.add(sns);
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 root = new Folder("D盘", 1);
root.add(im);
root.add(office);
System.out.println("=============show()=============");
root.show();
System.out.println("=============list()=============");
root.list();
}
}
运行结果如下所示。
==========安全组合模式=============
=============show()=============
D盘
+-即时聊天
+--QQ.exe
+--微信.exe
+--社交
+---两性
+----Momo.exe
+---职场
+----高端
+-----Boss直聘.exe
+----低端
+-----Maimai.exe
+-办公软件
+--Word.exe
+--PowerPoint.exe
+--Excel.exe
=============list()=============
即时聊天
办公软件
安全组合模式的好处就是接口定义职责清晰,符合设计模式的单一职责原则和接口隔离原则。缺点是用户需要区分树枝节点和叶子节点,这样才能正确处理各个层次的操作,客户端无法依赖抽象接口(Component),违背了设计模式的依赖倒置原则。所有教程
- C语言入门
- C语言编译器
- C语言项目案例
- 数据结构
- C++
- STL
- C++11
- socket
- GCC
- GDB
- Makefile
- OpenCV
- Qt教程
- Unity 3D
- UE4
- 游戏引擎
- Python
- Python并发编程
- TensorFlow
- Django
- NumPy
- Linux
- Shell
- Java教程
- 设计模式
- Java Swing
- Servlet
- JSP教程
- Struts2
- Maven
- Spring
- Spring MVC
- Spring Boot
- Spring Cloud
- Hibernate
- Mybatis
- MySQL教程
- MySQL函数
- NoSQL
- Redis
- MongoDB
- HBase
- Go语言
- C#
- MATLAB
- JavaScript
- Bootstrap
- HTML
- CSS教程
- PHP
- 汇编语言
- TCP/IP
- vi命令
- Android教程
- 区块链
- Docker
- 大数据
- 云计算