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

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

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<filesystemcomponent> 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 存储库
以上就是复合图案的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号