什么是复合模式?
复合模式是一种结构模式,允许您将对象组合成树结构来表示整体-部分层次结构。复合让客户可以统一处理单个对象和对象组合。
在复合模式中,有子元素的元素称为节点,没有子元素的元素称为叶子。
什么时候使用它?
- 当您需要整体、部分、父子或树状层次结构时,请使用复合模式。
- 当你想让客户端统一对待树中的子节点和父节点时,请使用复合模式。
问题
我们正在尝试实现简单的文件系统。首先,我们需要文件和目录类。但随着我们的文件结构变得越来越大,客户端将很难持续检查每个对象是哪些类的实例。
文件系统是树状层次结构的完美示例,我们希望统一对待文件和目录。是时候使用复合模式了!
解决方案

客户
由于 filesystemcomponent,客户端不关心对象是文件还是目录的实例。此外,客户端不必编写 if 语句来确定他是否在正确的对象上调用正确的方法。文件系统组件
文件和目录被客户端视为文件系统组件。 filesystemcomponent 定义方法的默认行为,默认行为可以是异常、不执行任何操作、返回 null 或 false,任何对您的应用程序有意义的行为。文件
这是我们的叶子,重写打印方法,打印其名称和内容。目录
这是我们的节点,重写添加、删除、获取子节点的方法。 print 方法打印出它的名称并调用子组件的 print 方法,这样 client 就不需要调用每个组件的 print 方法。
结构

java实现
public abstract class filesystemcomponent {
protected string name;
public filesystemcomponent(string name) {
this.name = name;
}
public string getname() {
return name;
}
public void print(int indentlevel) {
throw new unsupportedoperationexception();
}
public void add(filesystemcomponent component) {
throw new unsupportedoperationexception();
}
public void remove(int index) {
throw new unsupportedoperationexception();
}
// instead of throwing exception, we do nothing by default.
// doing nothing makes sense because leaf has no child.
public void getchildren() {
}
}
public class file extends filesystemcomponent {
private string content;
public file(string name, string content) {
super(name);
this.content = content;
}
@override
public void print(int indentlevel) {
string indent = " ".repeat(indentlevel);
system.out.println(indent + name + ": " + content);
}
}
public class directory extends filesystemcomponent {
private list children;
public directory(string name) {
super(name);
children = new arraylist<>();
}
@override
public void print(int indentlevel) {
string indent = " ".repeat(indentlevel);
system.out.println(indent + name + " directory:");
for (filesystemcomponent child : children) {
child.print(indentlevel + 2);
}
}
@override
public void add(filesystemcomponent component) {
children.add(component);
}
@override
public void remove(int index) {
children.remove(index);
}
@override
public void getchildren() {
if (children.isempty()) {
return;
}
stringbuilder builder = new stringbuilder("[");
for (filesystemcomponent child : children) {
builder.append(child.getname() + ", ");
}
builder.delete(builder.length() - 2, builder.length());
builder.append("]");
system.out.println(builder);
}
}
public class filesystemtestdrive {
public static void main(string[] args) {
filesystemcomponent rootdirectory = new directory("root");
filesystemcomponent fruitsdirectory = new directory("fruits");
filesystemcomponent animaldirectory = new directory("animal");
filesystemcomponent felinedirectory = new directory("feline");
rootdirectory.add(fruitsdirectory);
rootdirectory.add(animaldirectory);
fruitsdirectory.add(new file("appple", "red and juicy."));
fruitsdirectory.add(new file("banana", "yellow and sweet."));
fruitsdirectory.add(new file("lemon", "yellow and sour."));
animaldirectory.add(felinedirectory);
felinedirectory.add(new file("lion", "king of animal."));
felinedirectory.add(new file("tiger", "has cool color pattern."));
rootdirectory.print(0);
rootdirectory.getchildren();
rootdirectory.remove(0);
rootdirectory.getchildren();
// what happens we call getchildren() on leaf? (we don't override the method in leaf class)
filesystemcomponent file = new file("sample", "this is leaf");
file.getchildren(); // leaf calls default behavior, doing nothing
}
}
输出:
Root directory:
Fruits directory:
Appple: red and juicy.
Banana: yellow and sweet.
Lemon: yellow and sour.
Animal directory:
Feline directory:
Lion: King of animal.
Tiger: Has cool color pattern.
[Fruits, Animal]
[Animal]
陷阱
- 如果您想要一个组合使其子级保持特定顺序,则需要更复杂的管理方案来添加、删除和遍历子级。
- 随着复合结构变得更大、更复杂,遍历的成本会更高。在这种情况下,您可能会考虑实现一个缓存来存储一些数据以节省遍历。
您可以在这里查看所有设计模式的实现。
github 存储库










