diff --git a/README.md b/README.md index 38e48c9..aaa3ee7 100644 --- a/README.md +++ b/README.md @@ -44,7 +44,7 @@ 15.备忘录模式 - 已完成 -16.组合模式 - 整理中 +16.组合模式 - 已完成 17.原型模式 - 整理中 diff --git a/src/main/com/mxy/design/composite/AbsFile.java b/src/main/com/mxy/design/composite/AbsFile.java new file mode 100644 index 0000000..341c5e8 --- /dev/null +++ b/src/main/com/mxy/design/composite/AbsFile.java @@ -0,0 +1,7 @@ +package com.mxy.design.composite; + +public abstract class AbsFile { + + protected abstract void print(); + +} diff --git a/src/main/com/mxy/design/composite/File.java b/src/main/com/mxy/design/composite/File.java new file mode 100644 index 0000000..07ec131 --- /dev/null +++ b/src/main/com/mxy/design/composite/File.java @@ -0,0 +1,22 @@ +package com.mxy.design.composite; + +import com.google.common.base.Strings; + +public class File extends AbsFile { + + + private String fileName; + private int level; + + public File(String fileName, int level) { + this.fileName = fileName; + this.level = level; + } + + @Override + protected void print() { + System.out.println(Strings.repeat("-",level)+"|:"+fileName); + } + + +} diff --git a/src/main/com/mxy/design/composite/Folder.java b/src/main/com/mxy/design/composite/Folder.java new file mode 100644 index 0000000..831b80d --- /dev/null +++ b/src/main/com/mxy/design/composite/Folder.java @@ -0,0 +1,65 @@ +package com.mxy.design.composite; + +import com.google.common.base.Strings; +import com.google.common.collect.Lists; + +import java.util.ArrayList; +import java.util.List; + +public class Folder extends AbsFile { + + private String folderName; + private int level; + List folders = new ArrayList<>(); + List files = new ArrayList<>(); + + public Folder(String folderName, int level) { + this.folderName = folderName; + this.level = level; + } + + public int getLevel() { + return level; + } + + public void setLevel(int level) { + this.level = level; + } + + public List getFiles() { + return files; + } + + public void setFiles(List files) { + this.files = files; + } + + public String getFolderName() { + return folderName; + } + + public void setFolderName(String folderName) { + this.folderName = folderName; + } + + public Folder(AbsFile... file) { + files.addAll(Lists.newArrayList(file)); + } + + + public void add(AbsFile folder) { + folders.add(folder); + } + + @Override + protected void print() { + folders.addAll(files); + for (AbsFile file : folders) { + if (file instanceof Folder) { + System.out.println(Strings.repeat("-", ((Folder) file).getLevel()) + ":" + ((Folder) file).getFolderName()); + }else { + file.print(); + } + } + } +} diff --git a/src/test/com/mxy/design/composite/CompositeTest.java b/src/test/com/mxy/design/composite/CompositeTest.java new file mode 100644 index 0000000..7f4e973 --- /dev/null +++ b/src/test/com/mxy/design/composite/CompositeTest.java @@ -0,0 +1,36 @@ +package com.mxy.design.composite; + +import org.junit.Test; + +/** + * 组合模式:又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。 + * JDK 中 Map#PutAll List#addAll 使用这种模式 + * 场景:递归,树形结构,has a等场景 + */ +public class CompositeTest { + + @Test + public void logicTest() { + + File file3 = new File("设计模式.pdf",3); + File file2 = new File("Core java.pdf",3); + File file1 = new File("Java自学技巧.pdf",3); + + Folder folder = new Folder(file1,file2,file3); + folder.add(new Folder("收藏夹",1)); + folder.add(new Folder("Java自学",2)); + + folder.print(); + + file2 = new File("火影忍者.pdf",3); + file1 = new File("七龙珠.pdf",3); + folder = new Folder(file1,file2); + folder.add(new Folder("漫画",2)); + + folder.print(); + + + } + + +}