5.现欲构造一文件/目录树,采用组合(Composite)设计模式来设计,得到的类图如6-1所示:
{图}
图6-1类图
【Java代码】
import java.util.ArrayList;
import.java.util.List;
(1)class AbstractFile{
protected String name;
public void printName( ){System.out.println(name);}
public abstract boolean addChild(AbstractFile file);
public abstract boolean removeChild(AbstracFile file);
public abstract List<AbstractFile>getChildren( );
}
class File extends AbstractFile{
public File(String name){this.name=name;}
public?boolean addChild(AbstractFile file){return false;}
public?boolean?removeChild(AbstracFile file){return false;}
public?List<AbstractFile>getChildren( ){return(2);}
}
class Folder extends AbstractFile{
private List<AbstracFile>childList;
public Folder(String name){
this.name=name;
this.childList=new ArrayList<AbstractFile>( );
}
public boolean addChild(AbstractFile file){return childList.add(file);}
public boolean removeChild(AbstractFile file){return childList.remove(file);}
public(3)<AbstractFile>getChildren( ){return(4);}
}
public class Client{
public static void main(String[]args){
//创造一个树形的文件/目录结构
AbstractFile rootFolder=new Folder(“c:\\”);
AbstractFile compositeFolder=new Folder(“composite”);
AbstractFile windowsFolder=new Folder(“windows”);
AbstractFile file=new File(“TestComposite.java”);
rootFolder.addChild(compositeFolder);
rootFolder.addChild(windowsFolder);
compositeFolder.addChild(file);
//打印目录文件数
printTree(rootFolder);
}
private static void printTree(AbstractFile ifile){
ifile.printName( );
List<AbstractFile>children=ifile.getChildren( ):
if(Children==null)return;
for(AbstractFile file:Children){
(5);
}
}
}
该程序运行后输出结果为:
c:\
composite
TestComposite.java
Windows
【Java代码】
import java.util.ArrayList;
import.java.util.List;
(1)class AbstractFile{
protected String name;
public void printName( ){System.out.println(name);}
public abstract boolean addChild(AbstractFile file);
public abstract boolean removeChild(AbstracFile file);
public abstract List<AbstractFile>getChildren( );
}
class File extends AbstractFile{
public File(String name){this.name=name;}
public?boolean addChild(AbstractFile file){return false;}
public?boolean?removeChild(AbstracFile file){return false;}
public?List<AbstractFile>getChildren( ){return(2);}
}
class Folder extends AbstractFile{
private List<AbstracFile>childList;
public Folder(String name){
this.name=name;
this.childList=new ArrayList<AbstractFile>( );
}
public boolean addChild(AbstractFile file){return childList.add(file);}
public boolean removeChild(AbstractFile file){return childList.remove(file);}
public(3)<AbstractFile>getChildren( ){return(4);}
}
public class Client{
public static void main(String[]args){
//创造一个树形的文件/目录结构
AbstractFile rootFolder=new Folder(“c:\\”);
AbstractFile compositeFolder=new Folder(“composite”);
AbstractFile windowsFolder=new Folder(“windows”);
AbstractFile file=new File(“TestComposite.java”);
rootFolder.addChild(compositeFolder);
rootFolder.addChild(windowsFolder);
compositeFolder.addChild(file);
//打印目录文件数
printTree(rootFolder);
}
private static void printTree(AbstractFile ifile){
ifile.printName( );
List<AbstractFile>children=ifile.getChildren( ):
if(Children==null)return;
for(AbstractFile file:Children){
(5);
}
}
}
该程序运行后输出结果为:
c:\
composite
TestComposite.java
Windows