组合模式
组合模式(Composite Pattern)中通过组合多个对象形成树型结构以表示“整体--部分”的结构层次。组合模式对单个对象(叶子对象)和组合对象(容器对象)的使用具有一致性。组合模式又称为“整体----部分”(Part--Whole)模式。
Component:表示抽象构建,可以是接口或抽象类;
Leaf:表示叶子构建,在组合中表示叶子对象,叶子节点没有子节点,它实现了抽象接口构建接口声明的基本行为;
Composite:表示容器构建,它定义包含子节点的构建的行为,并存储子节点,它实现抽象构建接口中定义的操作叶子构建的行为;
抽象构件:抽象文件类
package Abstraction;
public abstract class AbstractFile {
public abstract void add(AbstractFile element);
public abstract void remove(AbstractFile element);
public abstract void display();
}
容器构件:文件夹类
package Component;
import java.util.ArrayList;
import Abstraction.AbstractFile;
public class Folder extends AbstractFile {
private ArrayList fileList=new ArrayList();
private String fileName;
public Folder(String fileName){
this.fileName=fileName;
}
@Override
public void add(AbstractFile element) {
fileList.add(element);
}
@Override
public void display() {
System.out.println("文件夹"+fileName+"-包含如下资源:");
for(Object obj:fileList){
((AbstractFile)obj).display();
}
}
@Override
public void remove(AbstractFile element) {
this.remove(element);
}
}
叶子构件:ImageFile
package Component;
import Abstraction.AbstractFile;
public class ImageFile extends AbstractFile {
private String fileName;
public ImageFile(String fileName){
this.fileName=fileName;
}
@Override
public void add(AbstractFile element) {
System.out.println("对不起,不支持该方法");
}
@Override
public void display() {
System.out.println("浏览图片"+fileName);
}
@Override
public void remove(AbstractFile element) {
System.out.println("对不起,不支持该方法");
}
}
叶子构件:VideoFile
package Component;
import Abstraction.AbstractFile;
public class VideoFile extends AbstractFile {
private String fileName;
public VideoFile(String fileName){
this.fileName=fileName;
}
@Override
public void add(AbstractFile element) {
System.out.println("对不起,不支持该方法");
}
@Override
public void display() {
System.out.println("浏览视频文件:"+fileName);
}
@Override
public void remove(AbstractFile element) {
System.out.println("对不起,不支持该方法");
}
}
叶子构件:TextFile
package Component;
import Abstraction.AbstractFile;
public class TextFile extends AbstractFile {
private String fileName;
public TextFile(String fileName){
this.fileName=fileName;
}
@Override
public void add(AbstractFile element) {
System.out.println("对不起,不支持该方法");
}
@Override
public void display() {
System.out.println("浏览文本文件"+fileName);
}
@Override
public void remove(AbstractFile element) {
System.out.println("对不起,不支持该方法");
}
}
Test:
package Test;
import Abstraction.AbstractFile;
import Component.Folder;
import Component.ImageFile;
import Component.TextFile;
import Component.VideoFile;
public class Test {
/**
* @param args
*/
public static void main(String[] args) {
AbstractFile file1,file2,file3,file4,file5,folder1,folder2,folder3;
file1=new ImageFile("房子.jpg");
file2=new ImageFile("美女.jpg");
file3=new TextFile("设计模式.txt");
file4=new TextFile("java程序设计.txt");
file5=new VideoFile("99999.rmvb");
folder1=new Folder("最新图片");
folder1.add(file1);
folder1.add(file2);
folder2=new Folder("学校资料");
folder2.add(file3);
folder2.add(file4);
folder3=new Folder("个人资料");
folder3.add(file5);
folder3.add(folder1);
folder3.add(folder2);
folder3.display();
}
}
测试结果: