Định nghĩa: the composite pattern is a partitioning design pattern. The composite pattern describes that a group of objects are to be treated in the same way as a single instance of an object. The intent of a composite is to “compose” objects into tree structures to represent part-whole hierarchies. Implementing the composite pattern lets clients treat individual objects and compositions uniformly.[1]
[code java=”lang”] <pre>public class Directory {
private Directory directory;
private File[] files;
public Directory(File[] files) {
this.files = files;
}
public Directory(Directory directory, File[] files) {
this.directory = directory;
this.files = files;
}
boolean isDirectory() {
return files != null;
}
boolean isFile() {
return files ==null;
}
public Directory getDirectory() {
return directory;
}
public void setDirectory(Directory directory) {
this.directory = directory;
}
public File[] getFiles() {
return files;
}
public void setFiles(File[] files) {
this.files = files;
}
}
[/code]
APR
2011