-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,7 +44,7 @@ | |
|
||
15.备忘录模式 - 已完成 | ||
|
||
16.组合模式 - 整理中 | ||
16.组合模式 - 已完成 | ||
|
||
17.原型模式 - 整理中 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
|
||
} | ||
|
||
|
||
} |