Skip to content

Commit

Permalink
feat:上传组合模式例子
Browse files Browse the repository at this point in the history
  • Loading branch information
maxy19 committed Mar 24, 2019
1 parent d258e23 commit 42d3551
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@

15.备忘录模式 - 已完成

16.组合模式 - 整理中
16.组合模式 - 已完成

17.原型模式 - 整理中

Expand Down
7 changes: 7 additions & 0 deletions src/main/com/mxy/design/composite/AbsFile.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mxy.design.composite;

public abstract class AbsFile {

protected abstract void print();

}
22 changes: 22 additions & 0 deletions src/main/com/mxy/design/composite/File.java
Original file line number Diff line number Diff line change
@@ -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);
}


}
65 changes: 65 additions & 0 deletions src/main/com/mxy/design/composite/Folder.java
Original file line number Diff line number Diff line change
@@ -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<AbsFile> folders = new ArrayList<>();
List<AbsFile> 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<AbsFile> getFiles() {
return files;
}

public void setFiles(List<AbsFile> 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();
}
}
}
}
36 changes: 36 additions & 0 deletions src/test/com/mxy/design/composite/CompositeTest.java
Original file line number Diff line number Diff line change
@@ -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();


}


}

0 comments on commit 42d3551

Please sign in to comment.