首页 > 编程笔记 > Java笔记

使用安全组合模式实现无限级文件系统

计算机是程序员每天都要接触的,计算机里的文件系统就是一个典型的树形结构,目录包含文件夹和文件,文件夹里又可以包含文件夹和文件。本节主要来实现一个简单的文件系统。

文件系统有两个大层次,即文件夹和文件。其中,文件夹能容纳其他层次,为树枝节点。文件是最小单位,为叶子节点。由于文件系统层次较少,树枝节点(文件夹)结构相对稳定,且文件可以有很多类型,所以我们选择使用安全组合模式来实现文件系统,可以避免为叶子节点类型(文件)引入冗余方法。

首先创建顶层的抽象组件 Directory 类。
  1. public abstract class Directory {
  2. protected String name;
  3.  
  4. public Directory(String name) {
  5. this.name = name;
  6. }
  7.  
  8. public abstract void show();
  9. }
然后分别创建 File 类和 Folder 类,File 类的代码如下。
  1. public class File extends Directory {
  2. public File(String name) {
  3. super(name);
  4. }
  5.  
  6. public void show() {
  7. System.out.println(this.name);
  8. }
  9. }
Folder 类的代码如下。
  1. public class Folder extends Directory {
  2.  
  3. private List<Directory> dirs;
  4. private Integer level;
  5.  
  6. public Folder(String name, Integer level) {
  7. super(name);
  8. this.level = level;
  9. this.dirs = new ArrayList<Directory>();
  10. }
  11.  
  12. public void show() {
  13. System.out.println(this.name);
  14. for (Directory c : dirs) {
  15. if (this.level != null) {
  16. for (int i = 0; i < this.level; i++) {
  17. System.out.print(" ");
  18. }
  19. for (int i = 0; i < this.level; i++) {
  20. if (i == 0) {
  21. System.out.print("+");
  22. }
  23. System.out.print("-");
  24. }
  25. }
  26. c.show();
  27. }
  28. }
  29.  
  30. public boolean add(Directory dir) {
  31. return this.dirs.add(dir);
  32. }
  33.  
  34. public boolean remove(Directory dir) {
  35. return this.dirs.remove(dir);
  36. }
  37.  
  38. public Directory get(int index) {
  39. return this.dirs.get(index);
  40. }
  41.  
  42. public void list() {
  43. for (Directory dir : dirs) {
  44. System.out.println(dir.name);
  45. }
  46. }
  47. }
可以看到,Folder 类不仅覆盖了顶层(父类)的 show() 方法,还增加了 list() 方法。最后我们来编写客户端测试代码。
  1. public class Test {
  2. public static void main(String[] args) {
  3. System.out.println("==========安全组合模式=============");
  4.  
  5. Folder im = new Folder("即时聊天", 2);
  6.  
  7. File qq = new File("QQ.exe");
  8. File wx = new File("微信.exe");
  9.  
  10. Folder sns = new Folder("社交", 3);
  11. Folder love = new Folder("两性", 4);
  12. Folder normal = new Folder("职场", 4);
  13. Folder high = new Folder("高端", 5);
  14. Folder low = new Folder("低端", 5);
  15.  
  16. File momo = new File("Momo.exe");
  17. love.add(momo);
  18.  
  19. File maimai = new File("Maimai.exe");
  20. low.add(maimai);
  21.  
  22. File boss = new File("Boss直聘.exe");
  23. high.add(boss);
  24.  
  25. normal.add(high);
  26. normal.add(low);
  27.  
  28. sns.add(love);
  29. sns.add(normal);
  30.  
  31. im.add(qq);
  32. im.add(wx);
  33. im.add(sns);
  34.  
  35. Folder office = new Folder("办公软件", 2);
  36.  
  37. File word = new File("Word.exe");
  38. File ppt = new File("PowerPoint.exe");
  39. File excel = new File("Excel.exe");
  40.  
  41. office.add(word);
  42. office.add(ppt);
  43. office.add(excel);
  44.  
  45. Folder root = new Folder("D盘", 1);
  46.  
  47. root.add(im);
  48. root.add(office);
  49.  
  50. System.out.println("=============show()=============");
  51. root.show();
  52.  
  53. System.out.println("=============list()=============");
  54. root.list();
  55. }
  56. }
运行结果如下所示。
==========安全组合模式=============
=============show()=============
D盘
   +-即时聊天
      +--QQ.exe
      +--微信.exe
      +--社交
         +---两性
            +----Momo.exe
         +---职场
            +----高端
               +-----Boss直聘.exe
            +----低端
               +-----Maimai.exe
   +-办公软件
      +--Word.exe
      +--PowerPoint.exe
      +--Excel.exe
=============list()=============
即时聊天
办公软件
安全组合模式的好处就是接口定义职责清晰,符合设计模式的单一职责原则和接口隔离原则。缺点是用户需要区分树枝节点和叶子节点,这样才能正确处理各个层次的操作,客户端无法依赖抽象接口(Component),违背了设计模式的依赖倒置原则。

所有教程

优秀文章