1、简述
组合模式(Composite Pattern)是一种结构型设计模式,旨在将对象组合成树形结构以表示“部分-整体”的层次结构。它使客户端对单个对象和组合对象的使用具有一致性。
设计模式样例:https://gitee.com/lhdxhl/design-pattern-example.git
2、什么是组合模式
组合模式的核心思想是将对象组织成树形结构,并通过一个统一的接口处理不同类型的节点(例如叶子节点和容器节点)。这种模式适用于需要以层次化方式组织数据的场景,例如文件系统、组织结构图等。
组合模式的优点:
- 层次结构清晰:通过树形结构组织对象,使整体结构更加清晰。
- 客户端统一操作:客户端无需关心对象是单个对象还是组合对象,操作方式一致。
- 扩展性强:可以方便地新增节点类型,而无需修改现有代码。
- 符合开闭原则:新增功能时,不影响已有功能。
3、实际案例
假设我们需要实现一个公司组织架构管理系统,其中有两类对象:普通员工(叶子节点)和部门(容器节点)。通过组合模式,我们可以统一处理单个员工和整个部门。
// 抽象组件
public interface Employee {
void showDetails();
}
// 叶子节点:普通员工
public class Developer implements Employee {
private String name;
private String position;
public Developer(String name, String position) {
this.name = name;
this.position = position;
}
@Override
public void showDetails() {
System.out.println(name + " - " + position);
}
}
public class Manager implements Employee {
private String name;
private String position;
public Manager(String name, String position) {
this.name = name;
this.position = position;
}
@Override
public void showDetails() {
System.out.println(name + " - " + position);
}
}
// 容器节点:部门
import java.util.ArrayList;
import java.util.List;
public class Department implements Employee {
private String name;
private List<Employee> employees = new ArrayList<>();
public Department(String name) {
this.name = name;
}
public void addEmployee(Employee employee) {
employees.add(employee);
}
public void removeEmployee(Employee employee) {
employees.remove(employee);
}
@Override
public void showDetails() {
System.out.println("Department: " + name);
for (Employee employee : employees) {
employee.showDetails();
}
}
}
// 测试代码
public class CompositePatternDemo {
public static void main(String[] args) {
Employee developer1 = new Developer("Alice", "Frontend Developer");
Employee developer2 = new Developer("Bob", "Backend Developer");
Employee manager1 = new Manager("Charlie", "Project Manager");
Department engineering = new Department("Engineering");
engineering.addEmployee(developer1);
engineering.addEmployee(developer2);
engineering.addEmployee(manager1);
Employee hr1 = new Manager("Diana", "HR Manager");
Department hr = new Department("Human Resources");
hr.addEmployee(hr1);
Department company = new Department("Company");
company.addEmployee(engineering);
company.addEmployee(hr);
company.showDetails();
}
}
输出结果
Department: Company
Department: Engineering
Alice - Frontend Developer
Bob - Backend Developer
Charlie - Project Manager
Department: Human Resources
Diana - HR Manager
4、应用场景
- 文件系统:文件和文件夹可以用组合模式表示,文件夹可以包含文件或子文件夹。
- 组织结构图:组织中的部门和员工之间具有“部分-整体”的关系。
- GUI 组件:如窗口、按钮、文本框等可以通过组合模式构建。
实际案例:文件系统
// 抽象组件
public interface FileSystemComponent {
void display();
}
// 叶子节点:文件
public class File implements FileSystemComponent {
private String name;
public File(String name) {
this.name = name;
}
@Override
public void display() {
System.out.println("File: " + name);
}
}
// 容器节点:文件夹
import java.util.ArrayList;
import java.util.List;
public class Folder implements FileSystemComponent {
private String name;
private List<FileSystemComponent> components = new ArrayList<>();
public Folder(String name) {
this.name = name;
}
public void addComponent(FileSystemComponent component) {
components.add(component);
}
public void removeComponent(FileSystemComponent component) {
components.remove(component);
}
@Override
public void display() {
System.out.println("Folder: " + name);
for (FileSystemComponent component : components) {
component.display();
}
}
}
// 测试代码
public class FileSystemDemo {
public static void main(String[] args) {
FileSystemComponent file1 = new File("file1.txt");
FileSystemComponent file2 = new File("file2.txt");
Folder folder1 = new Folder("Folder1");
folder1.addComponent(file1);
Folder folder2 = new Folder("Folder2");
folder2.addComponent(file2);
folder2.addComponent(folder1);
folder2.display();
}
}
输出结果
Folder: Folder2
File: file2.txt
Folder: Folder1
File: file1.txt
5、总结
组合模式通过统一的接口处理单个对象和组合对象,简化了复杂层次结构的处理。在开发中,当需要操作一个树形结构的对象时,组合模式是一种优秀的选择,可以显著提高代码的灵活性和可维护性。
评论区