谁可以帮我解释一下这个组合模式啊 还有就是组合模式的具体应用场合啊
希望用比较形象的说法 小弟真的不懂啊谢谢啦 package edu.scit.composite;import java.util.*;public interface IFile {
public void printName(); public boolean addChild(IFile file); public boolean removeChild(IFile file); public List<IFile> getChildren();
}//树形结构的叶子结点
class File implements IFile{
private String name;
public File(String name) {
this.name = name;
}
public void printName() {
System.out.println(name);
}
public boolean addChild(IFile file) {
return false;
}
public boolean removeChild(IFile file) {
return false;
}
public List<IFile> getChildren() {
return null;
} }//树形结构的枝节点
class Folder implements IFile {
private String name;
private List <IFile> childList;
public Folder(String name) {
this.name = name;
this.childList = new ArrayList<IFile>();
}
public void printName() {
System.out.println(name);
}
public boolean addChild(IFile file) {
return childList.add(file);
}
public boolean removeChild(IFile file) {
return childList.remove(file);
}
public List<IFile> getChildren() {
return childList;
}
}
class Client {
public static void main(String[] args) {
//构造一个树形的文件/目录结构
Folder rootFolder = new Folder("c:\\");
Folder compositeFolder = new Folder("composite");
rootFolder.addChild(compositeFolder);
Folder windowsFolder = new Folder("windows");
rootFolder.addChild(windowsFolder);
File file = new File("TestComposite.java");
compositeFolder.addChild(file);
//从rootFolder访问整个对象群
printTree(rootFolder);
}
private static void printTree(IFile ifile) {
ifile.printName();
List <IFile> children = ifile.getChildren();
for (IFile file:children) {
if (file instanceof File) {
System.out.print(" ");
file.printName();
} else if (file instanceof Folder) {
printTree(file);
}
}
}
}
希望用比较形象的说法 小弟真的不懂啊谢谢啦 package edu.scit.composite;import java.util.*;public interface IFile {
public void printName(); public boolean addChild(IFile file); public boolean removeChild(IFile file); public List<IFile> getChildren();
}//树形结构的叶子结点
class File implements IFile{
private String name;
public File(String name) {
this.name = name;
}
public void printName() {
System.out.println(name);
}
public boolean addChild(IFile file) {
return false;
}
public boolean removeChild(IFile file) {
return false;
}
public List<IFile> getChildren() {
return null;
} }//树形结构的枝节点
class Folder implements IFile {
private String name;
private List <IFile> childList;
public Folder(String name) {
this.name = name;
this.childList = new ArrayList<IFile>();
}
public void printName() {
System.out.println(name);
}
public boolean addChild(IFile file) {
return childList.add(file);
}
public boolean removeChild(IFile file) {
return childList.remove(file);
}
public List<IFile> getChildren() {
return childList;
}
}
class Client {
public static void main(String[] args) {
//构造一个树形的文件/目录结构
Folder rootFolder = new Folder("c:\\");
Folder compositeFolder = new Folder("composite");
rootFolder.addChild(compositeFolder);
Folder windowsFolder = new Folder("windows");
rootFolder.addChild(windowsFolder);
File file = new File("TestComposite.java");
compositeFolder.addChild(file);
//从rootFolder访问整个对象群
printTree(rootFolder);
}
private static void printTree(IFile ifile) {
ifile.printName();
List <IFile> children = ifile.getChildren();
for (IFile file:children) {
if (file instanceof File) {
System.out.print(" ");
file.printName();
} else if (file instanceof Folder) {
printTree(file);
}
}
}
}
interface Shape{ void draw();}
class Circle implements Shape{
public void draw(){ System.out.println("cicle");}
}
class Retangle implements Shape{
public void draw(){ System.out.println("retangle");}
}
class MoreShape implements Shape{
private List<Shape> shapes;
public MoreShape(List<Shape> shapes){this.shapes = shapes;}
public void draw(){
for(Shape s : shapes)
s.draw();
}
}在这里,对于Circle、Retangle和MoreShape的使用方法,无论是单一对象,还是多个对象,使用方法都是相同的。就是调用它的draw()方法。因为MoreShape也是Shape。