6.11. Combination mode

发布时间 : 2025-10-25 13:34:19 UTC      

Page Views: 10 views

Combination pattern (Composite Pattern), also known as partial whole pattern, is used to treat a group of similar objects as a single object. The combination pattern combines objects according to the tree structure, which is used to represent the partial and overall hierarchy. This type of design pattern is a structural pattern, which creates a tree structure of groups of objects.

This pattern creates a class that contains its own group of objects. This class provides a way to modify the same group of objects.

We demonstrate the use of the combination pattern through the following example. An example demonstrates the hierarchical structure of employees in an organization.

6.11.1. Introduction

意图: Objects are combined into a tree structure to represent a “part-whole” hierarchy. The combination mode enables users to use individual objects and composite objects consistently.

主要解决: In the problem of our tree structure, it blurs the concepts of simple elements and complex elements, and the client program can deal with complex elements like simple elements, thus decoupling the client program from the internal structure of complex elements.

何时使用: 1. You want to represent the part of the object-the overall hierarchy (tree structure). 2. You want the user to ignore the difference between the composite object and a single object, and the user will use all the objects in the composite structure uniformly.

如何解决: The branches and leaves achieve a unified interface, and the interface is combined within the branches.

关键代码: The interface is assembled inside the branch and contains the internal attribute List with Component in it.

应用实例: 1. Arithmetic expressions include operands, operators, and another Operand, where another Operand can also be an Operand, an operator, and another Operand. 2. In JAVA AWT and SWING, for Button and Checkbox, there are leaves, and Container is a branch.

优点: 1. The high-level module is easy to call. 2. Nodes increase freely.

缺点: When using the composite pattern, its leaves and branches are declared as implementation classes, not interfaces, violating the principle of dependency inversion.

使用场景: Partial, overall scene, such as tree menu, file, folder management.

注意事项: It is defined as a concrete class.

6.11.2. Realize

We have a class Employee This class is treated as a composite model class CompositePatternDemo Class usage Employee Class to add a departmental hierarchy and print all employees.

组合模式的 UML 图

6.11.3. Step 1

Create Employee Class with a Employee A list of objects.

Employee.java

importjava.util.ArrayList;importjava.util.List;publicclassEmployee{privateStringname;privateStringdept;privateintsalary;privateList<Employee>subordinates;//构造函数publicEmployee(Stringname,Stringdept,intsal){this.name=name;this.dept=dept;this.salary=sal;subordinates=newArrayList<Employee>();}publicvoidadd(Employeee){subordinates.add(e);}publicvoidremove(Employeee){subordinates.remove(e);}publicList<Employee>getSubordinates(){returnsubordinates;}publicStringtoString(){return("Employee :[ Name :"+name+", dept :"+dept+", salary :"+salary+"]");}} 

6.11.4. Step 2

Use Employee Class to create and print a hierarchy of employees.

CompositePatternDemo.java

publicclassCompositePatternDemo{publicstaticvoidmain(String[]args){EmployeeCEO=newEmployee("John","CEO",30000);EmployeeheadSales=newEmployee("Robert","Head Sales",20000);EmployeeheadMarketing=newEmployee("Michel","Head Marketing",20000);Employeeclerk1=newEmployee("Laura","Marketing",10000);Employeeclerk2=newEmployee("Bob","Marketing",10000);EmployeesalesExecutive1=newEmployee("Richard","Sales",10000);EmployeesalesExecutive2=newEmployee("Rob","Sales",10000);CEO.add(headSales);CEO.add(headMarketing);headSales.add(salesExecutive1);headSales.add(salesExecutive2);headMarketing.add(clerk1);headMarketing.add(clerk2);//打印该组织的所有员工System.out.println(CEO);for(EmployeeheadEmployee:CEO.getSubordinates()){System.out.println(headEmployee);for(Employeeemployee:headEmployee.getSubordinates()){System.out.println(employee);}}}} 

6.11.5. Step 3

Execute the program and the output is as follows:

Employee :[ Name : John, dept : CEO, salary :30000 ] Employee :[ Name : Robert, dept : Head Sales, salary :20000 ] Employee :[ Name : Richard, dept : Sales, salary :10000 ] Employee :[ Name : Rob, dept : Sales, salary :10000 ] Employee :[ Name : Michel, dept : Head Marketing, salary :20000 ] Employee :[ Name : Laura, dept : Marketing, salary :10000 ] Employee :[ Name : Bob, dept : Marketing, salary :10000 ] 
《地理信息系统原理、技术与方法》  97

最近几年来,地理信息系统无论是在理论上还是应用上都处在一个飞速发展的阶段。 GIS被应用于多个领域的建模和决策支持,如城市管理、区划、环境整治等等,地理信息成为信息时代重要的组成部分之一; “数字地球”概念的提出,更进一步推动了作为其技术支撑的GIS的发展。 与此同时,一些学者致力于相关的理论研究,如空间感知、空间数据误差、空间关系的形式化等等。 这恰好说明了地理信息系统作为应用技术和学科的两个方面,并且这两个方面构成了相互促进的发展过程。